Caesar's Cipher using python, could use a little help

后端 未结 8 1361
时光说笑
时光说笑 2020-11-30 14:24

I\'m trying to make a \"Caesar\'s Cipher\" while using python..this is what I have so far. Could anyone tell me how this is looking? Am I going in the right direction? What

相关标签:
8条回答
  • 2020-11-30 15:03

    This code should work pretty well. It also handles arbitrary offsets, including negative.

    phrase = raw_input("Please enter plaintext to Cipher: ")
    shift = int(raw_input("Please enter shift: "))
    
    result = ''
    for char in phrase:
        x = ord(char)
    
        if char.isalpha():
            x = x + shift
    
            offset = 65
            if char.islower():
                offset = 97
    
            while x < offset:
                x += 26
    
            while x > offset+25:
                x -= 26
    
            result += chr(x)
    
    print result
    

    The other way to do it, with a slightly different cipher, is simply rotate through all characters, upper and lower, or even all ascii > 0x20.

    phrase = raw_input("Please enter plaintext to Cipher: ")
    shift = int(raw_input("Please enter shift: "))
    
    result = ''
    for char in phrase:
        x = ord(char)
    
        x = x + shift
    
        while x < 32:
            x += 96
    
        while x > 127:
            x -= 96
    
        result += chr(x)
    
    print result
    
    0 讨论(0)
  • 2020-11-30 15:11

    I like kaizer.se's answer, but I think I can simplify it using the string.maketrans function:

    import string
    
    first = raw_input("Please enter Plaintext to Cipher: ")
    k = int(raw_input("Please enter the shift: "))
    
    shifted_lowercase = ascii_lowercase[k:] + ascii_lowercase[:k]
    
    translation_table = maketrans(ascii_lowercase, shifted_lowercase)
    
    print first.translate(translation_table)
    
    0 讨论(0)
  • 2020-11-30 15:18

    Barring the syntax errors, your code seems to work.

    However, I took the liberty of removing all duplicates, and cleaning it up:

    first = raw_input("Please enter Plaintext to Cipher: ")
    k = int(raw_input("Please enter the shift: "))
    
    result = ''
    for second in first:
        x=ord(second)
        x=x+k
        if x>90 and x<122:
            x=x-26
        elif x>122:
            x=x-26
        result += chr(x)
    
    print first    
    print result
    

    Also "first" and "second" are really bad names for those variables. "Input" and "letter" is probably better.

    0 讨论(0)
  • 2020-11-30 15:21

    Here is a oneliner.

    >>> brutus=lambda message,cipher,direction:''.join([chr((ord(letter)+(cipher*direction))%256) for letter in message])
    >>> encrypted= brutus('Message to be encrypted',14,1) #direction=1 for encryption
    >>> encrypted
    '[s\x81\x81ous.\x82}.ps.s|q\x80\x87~\x82sr'
    >>> brutus(encrypted,14,-1) # direction=-1 for decryption
    'Message to be encrypted'
    >>>
    
    0 讨论(0)
  • 2020-11-30 15:22

    For Python 3.3, try using the ord(), chr() and .isalpha functions:

    m = input("What is your message?: ")
    s = int(input("What is the shift?: "))
    for i in m:
        if i.isalpha():
            if (ord(i)+s)>90:
                print(chr(ord(i)+s-26),end=""),
            elif chr(ord(i)+s-26)<65:
                print("The shift is invalid")
            else:
                print(chr(ord(i)+s),end=""),
        else:
            pass
    
    0 讨论(0)
  • 2020-11-30 15:23

    Put a comma after each print statement; it will still put a space between the characters, but they'll all be on the same line. If you need to print them without the spaces, build them all into a single string and print that at the end.

    0 讨论(0)
提交回复
热议问题