Python Forum
Go up script/menu(a goto command)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Go up script/menu(a goto command)
#1
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 :(
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It would be beneficial to read this thread https://python-forum.io/Thread-Is-there-...s-in-BASIC
Recommended Tutorials:
Reply
#4
(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
Reply
#5
#!/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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
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()
Reply
#7
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.
Reply
#8
You missed the colon at the end of the line.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 12 571 Apr-12-2024, 11:51 AM
Last Post: deanhystad
  Is possible to run the python command to call python script on linux? cuten222 6 710 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Run CMD Command In Python Script shantanu97 2 6,490 May-13-2021, 08:08 AM
Last Post: Gribouillis
  goto jmabrito 34 10,054 Feb-18-2021, 09:55 PM
Last Post: jmabrito
  Script works when executed from command prompt but not when executed in SDP Tippex 0 1,996 Apr-07-2020, 04:26 PM
Last Post: Tippex
  How to make input goto a different line mxl671 2 2,446 Feb-04-2020, 07:12 PM
Last Post: Marbelous
  Script won't continue after command line skip671 7 4,839 Oct-15-2018, 07:37 PM
Last Post: snippsat
  Pyinstaller to execute a script in command prompt Ciroxxx 0 2,074 Sep-18-2018, 10:17 AM
Last Post: Ciroxxx
  goto problem Skaperen 1 2,725 Jan-27-2018, 01:11 PM
Last Post: stranac
  Is there a goto like there is in BASIC? Luke_Drillbrain 24 14,534 Apr-24-2017, 06:00 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020