Hi guys, I'm learning python right now, and I'm having a little trouble with one of my assignments. I have to write a program that will take a word and "encrypt" it. an example, if the letter is 'a' then it should get converted to 'b'. I have the code done for the first part (first assignment was just to do one character) right here
def translateChar(char,key):
abc=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
if char in abc:
str=ord(char)
if str > 64 and str < 91:
if key == 'a':
str+=0
char=chr(str)
return char
elif key == 'b':
str+=1
if str > 90:
temp=str-90
str=64+temp
char=chr(str)
return char
elif key == 'c':
str+=2
if str > 90:
temp=str-90
str=64+temp
char=chr(str)
return char
else:
return 'Key out of Range'
elif str > 96 and str < 123:
if key == 'a':
str+=0
char=chr(str)
return char
elif key == 'b':
str+=1
if str > 122:
temp=str-122
str=96+temp
char=chr(str)
return char
elif key == 'c':
str+=2
if str > 122:
temp=str-122
str=96+temp
char=chr(str)
return char
else:
return 'Key out of Range'
else:
return 'Character must be in the alphabet'
so for the second one I have to let take a whole word (or sentence) and it should still convert them all the same way. I tried using the above code with a for loop, heres the new code
def translatePhrase(phrase,key):
abc=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
for char in phrase:
print char
if char in abc:
str=ord(char)
if str > 64 and str < 91:
if key == 'a':
str+=0
char=chr(str)
elif key == 'b':
str+=1
if str > 90:
temp=str-90
str=64+temp
char=chr(str)
elif key == 'c':
str+=2
if str > 90:
temp=str-90
str=64+temp
char=chr(str)
else:
return 'Key out of Range'
elif str > 96 and str < 123:
if key == 'a':
str+=0
char=chr(str)
elif key == 'b':
str+=1
if str > 122:
temp=str-122
str=96+temp
char=chr(str)
print char
elif key == 'c':
str+=2
if str > 122:
temp=str-122
str=96+temp
char=chr(str)
else:
return 'Key out of Range'
else:
return 'Character must be in the alphabet'
print phrase
As its running through I can see each character gets changed (print statements in the code for debugging) but when it gets to the end it doesn't actually save the changes it made. I'm trying to get help on my school website, but answers are really slow on there. Any help would be greatly appreciated