0

I want to know a suitable loop for this code, to take it back to A after it reaches Z, i am very new to python and this would help me out a lot, thanks.

reset = "yes"
def encrypt():
 answer = ""
 for letter in message:
 new=ord(letter)+ offset
 answer=answer+chr(new)
 print(answer)
def decrypt():
 answer = ""
 for letter in message:
 new=ord(letter)- offset
 answer=answer+chr(new)
 print(answer)
while reset == "yes": 
 message= input("Type in a message.")
 offset= int(input("How many do you want offset the letters by?"))
 reset = "yes"
 while offset > 26:
 offset= int(input("How many do you want offset the letters by?(0-26)"))
 eord=input("Type E for encrypt, or D for decrypt")
 if eord == "e":
 encrypt()
 elif eord == "d":
 decrypt()
 else:
 eord=input("Type E for encrypt, or D for decrypt")
 reset= input("do you want to do another?")
asked Feb 10, 2016 at 14:03
2
  • You are asking for 'E' or 'D', but checking for 'e' or 'd'. On your last line you should probably tell them to type 'yes' (rather than 'Yes' or 'Y') Commented Feb 10, 2016 at 14:11
  • Also you need to pass the message to the encypt and decrypt functions. Ex: define def decrypt(message): and call with decrypt(message). Commented Feb 10, 2016 at 14:12

1 Answer 1

0

As I mentioned in comments, you are asking for 'E' or 'D', but checking for 'e' or 'd'. On your last line you should probably tell them to type 'yes' (rather than 'Yes' or 'Y'). Also you need to pass the message and offset variables to the encypt and decrypt functions.

You mentioned that it should recycle back to A from Z (rather than using non-alphabet characters in the shift). For that, you want to test the ord value using if statements, like this example:

for letter in message:
 # ..removed some code..
 # This part in particular is what you want (for both A and a)
 elif ord(letter) == 122: # 'z' has ord value of 122
 new = 'a'
 # and so on ...

Here is an edited version of your code. All it needs is the ord check mentioned above in your definitions of the encrypt and decrypt functions:

reset = "yes"
def encrypt(message, offset):
 answer = ""
 for letter in message:
 new=ord(letter)+ offset
 answer=answer+chr(new)
 print(answer)
def decrypt(message, offset):
 answer = ""
 for letter in message:
 new=ord(letter)- offset
 answer=answer+chr(new)
 print(answer)
while reset == "yes": 
 message= input("Type in a message.")
 offset= int(input("How many do you want offset the letters by?"))
 while offset > 26:
 offset= int(input("How many do you want offset the letters by?(0-26)"))
 eord=input("Type E for encrypt, or D for decrypt")
 while eord != 'E' or eord != 'D':
 if eord == "E":
 encrypt(message, offset)
 elif eord == "D":
 decrypt(message, offset)
 reset= input("do you want to do another (type 'yes')?")
answered Feb 10, 2016 at 14:17
Sign up to request clarification or add additional context in comments.

4 Comments

Jon, i have tried the code above and it comes up with this... Type in a message.hello How many do you want offset the letters by?5 Type E for encrypt, or D for decrypt e Type E for encrypt, or D for decrypt e do you want to do another (type 'yes')?
Try a capital letter E
Also, try a while loop instead of the if..elif..else loop you have there.
Something like while eord != 'E' or eord != 'D': ... edited my answer to show

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.