Search code examples
pythonlineback

Python - getting back to the line with input


I don't know how to go back to a line in python, how do I do that?

filesize = input("Podaj rozmiar pliku i jednostke (np. \"100 mb\", \"130 gb\", \"84 kb\"): ")

if filesize[-3:] ==  ' mb' or filesize[-3:] == ' kb' or filesize[-3:] == ' gb':
    print("true")
else:
    print("Podaj jeszcze raz rozmiar pliku w formacie [liczba format] np. 12 kb")
    #AND HERE I WANT TO TAKE IT BACK UP TO filesize BECAUSE ITS AN ERROR MSG

Solution

  • An easy way to do this is with a while loop and a flag. This will run the first time and will continue to ask for input until valid input is given, at which point it will continue.

    badInput = True
    while badInput:
        filesize = input("Podaj rozmiar pliku i jednostke (np. \"100 mb\", \"130 gb\", \"84 kb\"): ")
        if filesize[-3:] ==  ' mb' or filesize[-3:] == ' kb' or filesize[-3:] == ' gb':
            badInput = False
            print("true")
        else:
            print("Podaj jeszcze raz rozmiar pliku w formacie [liczba format] np. 12 kb")