1

I just started to learn python by myself, and i made a basic calculator.

However, i want to let the script go to the start if the user press a button that there's no option on the script, for example:

'1 Addition'

'2 Substract'

'3 Multiply'

'4 Divide'

The option 5 does not exist, so i want to make that when the user press '5' the scripts prints 'Error' and then the script returns to the start, printing the options i writed above. For example, like in a batch file, when you want to go at the start of the script after do something, you use:

goto start

start:

(options)

This is my code:

    print ('Choose something')
print ('1 Addition')
print ('2 Substract')
print ('3 Multiply')
print ('4 Divide')


option = input('Choose option');
number = int(input('First number '));
numbertwo = int(input('Second number'));
def addition (z, v) :
    return z + v;
def substract (f, h,) :
    return f - h;
def multiply (u, i,) :
    return u * i;
def divide (n, k) :
    return n / k;

if option == '1':
    print(numbertwo, "+" ,numbertwo, "=", addition(numero, numerodos));
elif option == '2':
    print(number, "-" ,numbertwo, "=", substract(number, numbertwo));
elif option == '3':
    print(number, "*" ,numbertwo, "=", multiply(number, numbertwo));
elif option == '4':
    print(number, "/" ,numbertwo, "=", divide(number, numbertwo));
elif option == '5':
    print("Error")

I tried to do it, but i just get the printed message and then the script ask for the numbers.

Any idea of how can i do it?

  • Put it in a loop. – Tom Karzes Mar 23 '20 at 19:22
  • Please review your educational materials. A "loop" is a common control flow construct, and is covered in any language tutorial. Stack Overflow is not a replacement for this reading. – Prune Mar 23 '20 at 19:22
  • Just continue through the tutorials, reading up on loops. – 9769953 Mar 23 '20 at 19:22
  • Python, being a language that was designed after 1960 and the structured programming revolution, does not have a GOTO statement. Instead, you control the flow of your program with things like loops and conditional branches, or even perhaps recursion. Stop thinking of scripts going to the "beginning". Think in terms of these control structures. – juanpa.arrivillaga Mar 23 '20 at 19:37

1 Answers1

0

There is no goto in python. However, you could use a while loop:

while True:
    print ('Choose something')
    print ('1 Addition')
    print ('2 Substract')
    print ('3 Multiply')
    print ('4 Divide')


    option = input('Choose option');
    if not option in ('1','2','3','4'):
        print("Error")
        continue # go to the start        
    number = int(input('First number '));
    numbertwo = int(input('Second number'));
    def addition (z, v) :
        return z + v;
    def substract (f, h,) :
        return f - h;
    def multiply (u, i,) :
        return u * i;
    def divide (n, k) :
        return n / k;

    if option == '1':
        print(numbertwo, "+" ,numbertwo, "=", addition(numero, numerodos));
    elif option == '2':
        print(number, "-" ,numbertwo, "=", substract(number, numbertwo));
    elif option == '3':
        print(number, "*" ,numbertwo, "=", multiply(number, numbertwo));
    elif option == '4':
        print(number, "/" ,numbertwo, "=", divide(number, numbertwo));
    break # terminate the loop otherwise
Arco Bast
  • 3,595
  • 2
  • 26
  • 53
  • I know there's no goto in python, it was an example, but i want to know how can i let the script go to the start (the options) when the user press the number 5 (the option 5 does not exists). –  Mar 23 '20 at 19:27
  • So you only want to ask the user again if the user presses 5? If so, see my edit. – Arco Bast Mar 23 '20 at 19:32
  • Yes, when the user press the number '5' i want to print something like 'Error no valid option' and then return to the main menu. EDIT: Thank you very much! The user is asked for put two numbers, but at least the script goes to the start, thanks! –  Mar 23 '20 at 19:32
  • So you might want to put the check right at the beginning, before the user is asked for two numbers. See my second edit. – Arco Bast Mar 23 '20 at 19:37