How to go to a specific line of code in python

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!!

+4

Create a function, then you can "jump" to that code whenever you need to.

Felix

0

If you want to repeat the execution of a code, put it in a loop.

Gershon Fosu

0

def repeatFunction(): # here goes the part that you want to repeat. def main(): while(condition here): repeatFunction() main()

Benedek Máté Tóth

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Text files are composed of plain text content. Text files are also known as flat files or plain files. Python provides easy support to read and access the content within the file. Text files are first opened and then the content is accessed from it in the order of lines. By default, the line numbers begin with the 0th index. There are various ways to read specific lines from a text file in python, this article is aimed at discussing them. 

    File in use: test.txt

    Method 1: fileobject.readlines()

    A file object can be created in Python and then readlines() method can be invoked on this object to read lines into a stream. This method is preferred when a single line or a range of lines from a file needs to be accessed simultaneously. It can be easily used to print lines from any random starting index to some ending index. It initially reads the entire content of the file and keep a copy of it in memory. The lines at the specified indices are then accessed. 

    Example:

    Python3

    file = open('test.txt')

    content = file.readlines()

    print("tenth line")

    print(content[9])

    print("first three lines")

    print(content[0:3])

    Output 

    tenth line
     

    This is line 10.

    first three lines
     

    This is line 1.This is line 2.This is line 3.

    Method 2: linecache package 

    The linecache package can be imported in Python and then be used to extract and access specific lines in Python. The package can be used to read multiple lines simultaneously. It makes use of cache storage to perform optimization internally. This package opens the file on its own and gets to the particular line. This package has getline() method which is used for the same. 

    Syntax: 

    getLine(txt-file, line_number)

    Example:

    Python3

    import linecache

    particular_line = linecache.getline('test.txt', 4)

    print(particular_line)

    Output :

    This is line 5.

    Method 3: enumerate()

    The enumerate() method is used to convert a string or a list object to a sequence of data indexed by numbers. It is then used in the listing of the data in combination with for loop. Lines at particular indexes can be accessed by specifying the index numbers required in an array. 

    Example:

    Python3

    file = open("test.txt")

    specified_lines = [0, 7, 11]

    for pos, l_num in enumerate(file):

        if pos in specified_lines:

            print(l_num)

    Output

    This is line 1. This is line 8. This is line 12.

    Chủ đề