开发者

Python: Caesar Cipher, If statement and else statement both True at the same time?

I pretty much have this solved but for some reason the first If statement and the else statement are both saying true for any capitalized letter inputs. So if the ASCII value lands between 65 and 90 the if statement declares true and prints out that value, but then the else statement also declares true and prints out a statement. If I put 'continue' at the bottom of the first if statement, this program works flawlessly. However I have no clue why it works like that. Can you please help me correct my flaw in logic, I dont understand why it is doing this. Here is my code and output:

k = int(raw_input("Please enter a value for k: ")) #Shifter number
original = raw_input("plaintext:  ") #Message user wants ciphered
original_as_array = list(original) ##I am turning the input into an array

for i in range(0,len(original)): ##Now seperating each character to add k
    char = original_as_array[i]
    charint = ord(char)

    if charint >= 65 and charint <=90:
        cipher_int = ((charint-65 + k) % 26)+65
        code_char = chr(cipher_int)
        print code_char,

    if charint >= 97 and charint <=122:
        cipher_int = ((charint-97 + k) % 26)+97
        code_char = chr(cipher_int)
        print code_char,


    else:
        print char,

Example output wi开发者_运维技巧th only caps:

Please enter a value for k: 13
plaintext:  PLEASE HELP
C P Y L R E N A F S R E   U H R E Y L C P


Your first if statement isn't related to the else statement. You want

if charint >= 65 and charint <=90:
        cipher_int = ((charint-65 + k) % 26)+65
        code_char = chr(cipher_int)
        print code_char,

elif charint >= 97 and charint <=122:
    cipher_int = ((charint-97 + k) % 26)+97
    code_char = chr(cipher_int)
    print code_char,


else:
    print char,

Otherwise (for capital letters) the first condition resolves to true, the second to false, and because the statement resolved to false the else statement gets executed.


You should also learn how to make your code more pythonic.

First things first: Python lists are not arrays, they are lists. Not the same thing.

You don't need to crate a list from your string. Python strings are sequences of characters and already support subscription:

>>> 'hello'[4]
'o'

But you need neither lists nor subscriptions to access the characters of your string. You can, and should, iterate like this:

for char in original:
    ...

Also, comparison operators in Python can, and should, be chained:

if 65 <= charint <= 90:
    ...

There's repetition going on. Don't repeat yourself:

def cipher(ch_int, offset, lowest):
    return chr((ch_int - lowest + offset) % 26 + lowest)

for char in original:
    charint = ord(char)
    if 65 <= charint <= 90:
        print cipher(charint, k, 65),
    elif 97 <= charint <= 122:
        print cipher(charint, k, 97),
    else:
        print char,


Your problem is that you need to use elif (see http://docs.python.org/tutorial/controlflow.html).

The else: clause runs if the letter isn't lower case.

By the way, you don't need to make the "original" a list. Strings in pythons behave pretty much identically to lists.


k = int(raw_input("Please enter a value for k: ")) #Shifter number original = raw_input("plaintext: ") #Message user wants ciphered original_as_array = list(original) ##I am turning the input into an array

for i in range(0,len(original)): ##Now seperating each character to add k char = original_as_array[i] charint = ord(char)

if charint >= 65 and charint <=90:
    cipher_int = ((charint-65 + k) % 26)+65
    code_char = chr(cipher_int)
    print code_char,

elif charint >= 97 and charint <=122:
    cipher_int = ((charint-97 + k) % 26)+97
    code_char = chr(cipher_int)
    print code_char,


else:
    print char,


Try this:

#!/usr/bin/python

k = int(raw_input("Please enter a value for k: ")) #Shifter number
original = raw_input("plaintext:  ") #Message user wants ciphered
original_as_array = list(original) ##I am turning the input into an array

for i in range(0,len(original)): ##Now seperating each character to add k
    char = original_as_array[i]
    charint = ord(char)

    if charint >= 65 and charint <=90:
        cipher_int = ((charint-65 + k) % 26)+65
        code_char = chr(cipher_int)
        print code_char,
        continue

    if charint >= 97 and charint <=122:
        cipher_int = ((charint-97 + k) % 26)+97
        code_char = chr(cipher_int)
        print code_char,
        continue

    print char,

After every successful case, you need to skip the other cases with continue.

As a side note:

This can also be done as follows:

>>> import string
>>> transtab = string.maketrans(string.lowercase + string.uppercase,string.lowercase[k:] + string.lowercase[:k] + string.uppercase[k:] + string.uppercase[:k])
>>> "PLEASE HELP".translate(t)
'CYRNFR URYC'


word=raw_input("ENTER YOUR MESSAGE IN CAPITAL LETTERS :")

def cal(a):
    if a=="A":
        c="D"
    elif a=="B":
        c="E"
    elif a=="C":
        c="F"
    elif a=="D":
        c="G"
    elif a=="E":
        c="H"
    elif a=="F":
        c="I"
    elif a=="G":
        c="J"
    elif a=="H":
        c="K"
    elif a=="I":
        c="L"
    elif a=="J":
        c="M"
    elif a=="K":
        c="N"
    elif a=="L":
        c="O"
    elif a=="M":
        c="P"
    elif a=="N":
        c="Q"
    elif a=="O":
        c="R"
    elif a=="P":
        c="S"
    elif a=="Q":
        c="T"
    elif a=="R":
        c="U"
    elif a=="S":
        c="V"
    elif a=="T":
        c="W"
    elif a=="U":
        c="X"
    elif a=="V":
        c="Y"
    elif a=="W":
        c="Z"
    elif a=="X":
        c="A"
    elif a=="Y":
        c="B"

    elif a=="Z":
        c="C"
    elif a==" ":
        c=" "
    else:
        c=a
    return c

b=len(word)
l=""
a=0
while a<b:
    l=l+cal(word[a])
    a=a+1

print l
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜