2016-03-26 21 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