2016-03-26 20 views
0

我在Python中有一个由多个函数列表组成的脚本,并且在列表的每一端我都要放置一个后退函数,让我返回到脚本的开头并选择另一个列表。例如:如何返回到python脚本的顶部?

list = ("1. List of all users", 
    "2. List of all groups", 
    "3. Reset password", 
    "4. Create new user", 
    "5. Create new group", 
    "6. List all kernel drivers", 
    "7. List all mounts", 
    "8. Mount a folder", 
    "9. Exit") 
for i in list: 
    print(i) 

如果我选择1另一个列表打开:

list = "1) Show user Groups \n2) Show user ID \n3) Show user aliases \n4) Add new aliases \n5) Change password \n6) Back" 
print 
print list 

更具体的例子。

+0

使用某种循环。你可以在这里学习如何做到这一点:http://www.tutorialspoint.com/python/python_while_loop.htm –

回答

0

您可以使用while循环,直到用户明确不退出程序。

import os 



def show_users(): 
    print("1) show group user") 
    print("2) go back") 

    i = int(input()) 
    if i==1: 
     pass # do something 
    elif i==2: 
     show_list() 

def show_list(): 
    print("1) list of all users") 
    print("2) exit") 
    i = int(input()) 

    if i ==1: 
     show_users() 
    elif i==2: 
     exit(0) 


while True: 
    show_list() 
0

你可以在while循环做到这一点,它只是不断通过这些选项运行,直到你回答4对第一个问题。您可以将一个while循环放在另一个循环中,以使其变得更加复杂。

keepGoing = True 
while keepGoing: 
    choice1 = raw_input('first answer') 
    if choice1 == '1': 
    choice2 = raw_input('second answer') 
    if choice2 == '1': 
     print('1 again') 
    else: 
     print('something different') 
    if choice1 == '2': 
    print('two') 
    if choice1 == 3: 
    print('three') 
    if choice1 == 4: 
    keepGoing = False