-1

I just pulled out some old code but I'm curious to know how to jump back to a specific line of code. What I mean by this is that if there is an if statement, it will do something unless told otherwise, anyways, what I want to do is when the if statement ends, or when I get to the else bit, I want the code to not start all over again but start at a certain line in the code. I will explain more below:

CODE:

def main():
    abc = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'
    message = input("What's the message to encrypt/decrypt? ")
    def keyRead():
        try:
            return int(input("What number would you like for your key value? "))
        except ValueError:
            print("You must enter a number!")
            main()
    key = keyRead()
    choice = input("Choose: encrypt or decrypt. ")
    if choice == "encrypt":
        encrypt(abc, message, key)
    elif choice == "decrypt":
        encrypt(abc, message, key * (-1))
    else:
        print("You must chose either 'encrypt' or 'decrypt!'")
        main()

def encrypt(abc, message, key):
    cipherText = ""
    for letter in message:
        if letter in abc:
            newPosition = (abc.find(letter) + key * 2) % 52
            cipherText += abc[newPosition]
        else:
            cipherText += letter
    print(cipherText)
    return cipherText

main()

So what I want really is that if the user doesn't input encrypt or decrypt it will show them the message: You must enter either 'encrypt' or 'decrypt'! but under this line I want it to go back to the choice part and not all the way back to the message part. If there is a way to do this I would really appreciate you helping me out!!

pvg
  • 2,673
  • 4
  • 17
  • 31
Kieran
  • 41
  • 1
  • 2
  • 9
  • 1
    You could wrap a loop around the code that asks the user for encrypt or decrypt. If they answer an accepted option then you can exit the loop. However, if the response is not encrypt or decrypt, the loop will continue thus asking the question again. – Kyle Sep 17 '17 at 18:20
  • Is `!=` the same as `is not`? Because this is what I'm doing: `while choice != "encrypt" or choice != "decrypt:"` and it doesn't seem to work too well :/ – Kieran Sep 17 '17 at 18:24
  • [Here](https://stackoverflow.com/questions/2209755/python-operation-vs-is-not) is an explanation between the two. – Kyle Sep 17 '17 at 18:27
  • @Kieran use `while True` and add `break` in the `if` statement and continue in the `else` statement.[Look here for more information](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – optimalic Sep 17 '17 at 19:37

1 Answers1

2

albeit loop best suited your case, but you really could jump back to a specific line:

import sys

def jump(lineno):
    frame = sys._getframe().f_back
    called_from = frame

    def hook(frame, event, arg):
        if event == 'line' and frame == called_from:
            try:
                frame.f_lineno = lineno
            except ValueError as e:
                print "jump failed:", e
            while frame:
                frame.f_trace = None
                frame = frame.f_back
            return None
        return hook

    while frame:
        frame.f_trace = hook
        frame = frame.f_back
    sys.settrace(hook)

use this function to jump back to line key = keyRead(). besides, there are goto implementation in the wild.

georgexsh
  • 15,984
  • 2
  • 37
  • 62