-5

I've looked through a few answers on here but none have worked for me, and I know there's probably a lot of things wrong with this code, but all I want to know is how to simply restart the code if the input at the END is 'Yes'

Code = input("Do you want to encrypt or decrypt? ")
Code = Code.upper()
Answer = ["ENCRYPT","DECRYPT"]

    if Code in Answer:

            Plaintext = input("What's your message? ")
            Plaintext = Plaintext.upper()
            Shift = int(input("What's the shift number? "))
            LengthPT = len(Plaintext)
            CodeLetter = ""

            if Code == ("ENCRYPT"):
                    for i in range (0,LengthPT):
                            Pletter = ord(Plaintext[i]) -64
                            Codeletter = Pletter + Shift
                            if Codeletter > 26:
                                    Codeletter = Codeletter - 26
                            Codeascii = chr(Codeletter + 64)
                            CodeLetter = CodeLetter + Codeascii

            elif Code == ("DECRYPT"):
                    for i in range (0,LengthPT):
                            Pletter = ord(Plaintext[i]) -64
                            Codeletter = Pletter - Shift
                            if Codeletter < 0:
                                    Codeletter = Codeletter + 26
                            Codeascii = chr(Codeletter + 64)
                            CodeLetter = CodeLetter + Codeascii
    else:
            print("Wrong answer.")

    if Code == ("ENCRYPT"):
            print("Encoded Message =", CodeLetter)

    elif Code == ("DECRYPT"):
            print("Decoded Message =", CodeLetter)

    Answer2 = input("Do you want to restart? (Yes/No): ")
Connor
  • 3
  • 3

2 Answers2

3

There are many ways, but this seems to be the shortest path:

Answer2 = "Yes"
while Answer2 == "Yes":
  ...
  Answer2 = input("Do you want to restart? (Yes/No): ")

Typically you might want to .lower ().strip () Answer2 too.

gauteh
  • 16,435
  • 4
  • 30
  • 34
  • 1
    Typically you'll want to get rid of the extra variable and just use a `while True:` loop. Then you `break` out of the loop if the answer is not "Yes". – Matthias Apr 25 '16 at 11:22
  • @gauteh Could you possibly provide an example of that solution added into the entire code, with the ellipse part filled in please? I neglected to mention that I'm quite new to all of this, so I'm not entirely sure how to implement your solution. – Connor Apr 28 '16 at 13:13
  • @Connor: You put all of your code in where the ellipsis is. And then fix the second Answer2-line if necessary. Experiment a bit with extremely simple `while` loops if that doesn't work (e.g. change the ellipsis to print ("a")). – gauteh Apr 28 '16 at 13:27
  • Thank you, I finally figured it out and got it working. Thanks for the help :) – Connor Apr 28 '16 at 13:49
-1

Make it all a function. Check if the answer is 'yes' and then call the function again. Recursion is fine in Python, so this will work.

TKoL
  • 13,158
  • 3
  • 39
  • 73
  • 3
    Recursion is not fine in Python and using functions as jump marks is wrong in every language. – Matthias Apr 25 '16 at 11:21
  • Python (sadly) does not have tail-call-optimization https://en.wikipedia.org/wiki/Tail_call .. So making the function recursive will limit you to the default stack size.. You can ofc use ugly hacks like trampoline techniques.. However, using recursion in Python feels like working against the language rather than with the language. – Michelrandahl Apr 25 '16 at 15:11
  • Thank you @Matthias and Mitzh , I didn't realize recursion was considered so bad in python. – TKoL Apr 26 '16 at 13:02