Python Forum

Full Version: Go up script/menu(a goto command)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i know there is not a goto or similar command in python,im trying to make a menu with submenus
a main menu that gives you games/math/managment options,and when you choose one of them it brings you to another menu where you can run a task
however i want there to be a option in the submenus that allows you to go back to main menu ,and/or after the task is finished for it to go back to its mother submenu
here is the basic code


#!/usr/bin/python
print ("\n" * 200)
print ("Welcome,what would you like to do?type the number")
print ("\n")
print ("[1]:Math solver")
print ("[2]:exit")
print ("\n")
menu = input(": ")
if menu = 1
  print ("\n" * 200)
  print ("What type is your question?")
  print ("\n" * 3)
  print ("[1]:Bisquared-equasion")
  print ("[2]:Exit")
  print ("\n")
  menu = input(": ")
  if menu = 1
if menu = 1 should run a task after which it should go back to the Math solver menu instead of quiting
how do i do that,im Really new so i need guidance and examples :(
Well, generally this would all be in a loop that would go until you got a 2 (exit). Then you could have a sub-loop for sub-menus.

I would suggest having each menu loop in it's own function, that returns when the loop is over. Then you would have something like:
if menu == 1:
    math_menu()
It help organize your code.
It would be beneficial to read this thread https://python-forum.io/Thread-Is-there-...s-in-BASIC
(Apr-24-2017, 04:48 PM)ichabod801 Wrote: [ -> ]Well, generally this would all be in a loop that would go until you got a 2 (exit). Then you could have a sub-loop for sub-menus.

I would suggest having each menu loop in it's own function, that returns when the loop is over. Then you would have something like:
if menu == 1:
    math_menu()
It help organize your code.

how will that look in my code,i have just started learning and can't get where/how to use the "math_menu()" part
i just need to see it in my code as i know what does what there,i tried reading that thread but i don't understand how to apply that to my script,thanks in advance ,and sorry for being inexperienced and dumb.not good at understanding others code
#!/usr/bin/python

def math_menu():
    while True:
        print ("\n" * 200)
        print ("What type is your question?")
        print ("\n" * 3)
        print ("[1]:Bisquared-equasion")
        print ("[2]:Exit")
        print ("\n")
        menu = int(input(": "))
        if menu == 1:
            bisquared()
        elif menu == 2:
            break

while True:
    print ("\n" * 200)
    print ("Welcome,what would you like to do?type the number")
    print ("\n")
    print ("[1]:Math solver")
    print ("[2]:exit")
    print ("\n")
    menu = int(input(": "))
    if menu == 1:
        math_menu()
    elif menu == 2:
        break
If change my code a little in link that metulburr post.
You see that it fit this kind of task well.
def solve():
   answer = int(input('What is 2 + 2? '))
   if answer == 4:
       print('\nCorrect')
   else:
       print('\nWrong')
   input('Push enter to retun to menu')

def menu():
   while True:
       print('(1) Solve something')
       print('(Q) Quit')
       choice = input('Enter your choice: ').lower()
       if choice == '1':
           solve()
       elif choice == 'q':
           return
       else:
           print('Not a correct choice: <{}>,try again'.format(choice))

if __name__ == '__main__':
  menu()
Certainly its me again and im EXTREMELY sorry for bothering so much but it gives me this error
Error:
 File "foxware 2.0.py", line 3     def math_menu()                   ^ SyntaxError: invalid syntax
am i supposed to paste the whole code in the brackets() or have i done something wrong elsewhere
this time im posting my whole code incase its because of a mistake elsewhere
#!/usr/bin/python

def math_menu()
  while True:
    print ("\n" * 200)
    print ("Welcome,what would you like to do?type the number")
    print ("\n")
    print ("[1]:Math solver")
    print ("[2]:exit")
    print ("\n")
    menu = input(": ")
    if menu == 1
      print ("\n" * 200)
      print ("What type is your question?")
      print ("\n" * 3)
      print ("[1]:Bisquared-equasion")
      print ("[2]:Exit")
      print ("\n")
      menu = int(input(": "))
      if menu == 1
    bisquared()
      import math
      print  ("Ax*2+Bx+C=0")
      print ("what is your A?")
      (a) = input('my a is: ')
      print ("what is your B?")
      (b) = input('my b is: ')
      print ("what is your C?")
      (c) = input('my c is: ')
      print ("calculating")
      a = float(a)
      b = float(b)
      c = float(c)
      b2 = b*b
      b2 = float(b2)
      d = b2-4*a*c
      d = float(d)
      if d < 0: 
          d = str(d)
          print ("\n" * 200)
          print (" D is Less than 0,There are no answers")
          d = str(d)
          exit
      d = float(d)
      if d >= 0:
          d = str(d)
          print ("D is: " + d )
          d = float(d)
          sqd = math.sqrt(d)
          sqd = float(sqd)
          x1a = -b+sqd
          x1a = float(x1a)
          x2a = -b-sqd
          x2a = float(x2a)
          ax2 = a*2
          ax2 = float(ax2)
          x1 = x1a/ax2
          x1 = str(x1)
          x2 = x2a/ax2
          x2 = str(x2)
          if d >= 0:
            d = str(d)
            print ("\n" * 200)
            print ("your X1 is " + x1 +  " your X2 is " + x2)
            print ("your discriminant is " + d )
            exit
      elif menu == 2 
    break
    if menu = 2
      exit
im really sorry again.
You missed the colon at the end of the line.