menu - Go to line in python? -


i typing python menu , wondering if there way make program return place. instance:

print 'choose: ' = raw_input (' apple[a], grape[g], quit[q] ') if a=='a':     print 'apple' elif a=='g':     print 'grape' elif a=='q':     print 'quit'     print 'are sure?'     print 'yes[y], no[n]'     b=raw_input ('choose: ')     if b=='y':         quit()     elif b=='n':       print 'returning menu' 

in part is:

`b=raw_input ('choose: ')     if b=='y':         quit()     elif b=='n':         print 'returning menu'` 

how return first apple\grape menu? there way user doesn't have quit , instead return main menu?

i either use function recursively or while loop. since there while loop solutions, recursive solution be:

from sys import exit  def menu():     = raw_input("choose: apple[a], grape[g], quit[q] ")     if == 'a':         return 'apple'     elif == 'g':         return 'grape'     elif == 'q':         print 'are sure want quit?'         b = raw_input ('choose: yes[y], no[n] ')         if b == 'y':             exit()         elif b == 'n':             return menu()  # calls function again, we're asked question "a" again  menu()  

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -