2017-05-30 53 views
-1

我正在寻找一种方法 - 在您选择了您想要查看的内容(main_screen_choice)之后 - 能够选择其他内容而无需重新启动程序。这里是我的代码Python刷新登录系统代码

users = [] 
victims = ['John Smith', 'Karen Jones', 'Kevin Tomlinson'] 
print ('>>>>> RauCo. Protection Corp. <<<<<') 
user = input('Username: ') 
def storeUser(): 
    users.append(user) 
def check(): 
    if user == 'Cabbage': 
    storeUser() 
    print ('Welcome back, Master...') 
    else: 
    print ('INTRUDER') 
    exit() 
check() 
main_screen_choice = input('What do you want to do? \nVictims, To Do, 
Exit\n(Case Sensitive!): ') 
def checkMainChoice(): 
if main_screen_choice == 'Victims': 
    print (victims) 
elif main_screen_choice == 'To Do': 
    print ('Have a shower - you stink!') 
elif main_screen_choice == 'Exit': 
    exit() 
else: 
    print ('Error: Not a valid option, please run program again') 
checkMainChoice() 
+0

再次调用'input'? –

+0

了解循环如何工作,例如https://www.learnpython.org/en/Loops并将你的输入封装到一个循环中。 – Milo

回答

1

只是反复回去的地方,你要求输入并执行相应的行为点。

这通常使用while循环来实现。由于是,你的代码应该是这样的:

while True: 
    main_screen_choice = input('What do you want to do? \nVictims, To Do, 
     Exit\n(Case Sensitive!): ') 
    checkMainChoice() 

但我不喜欢全局变量,我会建议你给参数传递给checkMainChoice功能:

def checkMainChoice(choice): 
    if choice == 'Victims': 
     print (victims) 
    elif choice == 'To Do': 
     print ('Have a shower - you stink!') 
    elif choice == 'Exit': 
     exit() 
    else: 
     print ('Error: Not a valid option, please run program again') 

然后, while循环会变成:

while True: 
    main_screen_choice = input(...) 
    checkMainChoice(main_screen_choice) 

一些言论作为一个侧面说明:

  • Python中的缩进通常是四个空格。此外,为了使代码可读,您确实需要跳过行。
  • Python中的用法是将mixedCase中的变量和lower_case_with_underscores中的方法/函数命名为变量。
  • 您通知该选项区分大小写,对于那种文本菜单来说这不太舒服。您可以通过使用lower方法将输入设置为小写来轻松解决此问题:choice = input(...).lower()。然后,您将输入与小写字符串进行比较:'to do','exit' ...
+0

添加while循环后,当我回答我想要做的事时,它只是再次问我我想做什么 –

+0

@TylerRauer好吧,如果这不是你要求的,那么你的问题并不是很清楚 –