2013-03-04 22 views
-3

我需要“命令”循环并能够在程序中随时使用我怎样才能让我的程序在命令后循环,并让每个命令在线之前的命令行之前运行

print('Type command or view login history.') 
command = input() 
if command == "loginhistory": 
    print('flynn 12:42 1/27/87') 

command = input() 
if command == "login": 
    print('Please type in your password, Flynn') 
+3

您好,欢迎光临。这个网站上有几个非常密切相关的问题。请看看周围。谢谢。 – bernie 2013-03-04 22:15:13

回答

1

虽然循环应该做的伎俩。我们将变量运行设置为True,循环将继续,直到我们不再希望它运行为止。然后,我们将运行设置为False来结束循环。

while run: 
    print('Type command or view login history.') 
    command = input() 
    if command == "loginhistory": 
    print('flynn 12:42 1/27/87') 
    elif command == "login": 
    print('Please type in your password, Flynn') 

当条件满足要在哪里结束循环时,将运行更改为false。例如,在最后一行之后,在while循环内添加:

elif command=="end": 
    print('You have decided to end the program') 
    #this will end the loop 
    run = False 
+1

我强烈推荐使用'while run:',因为'run'已经是一个布尔值。 – jpmc26 2013-03-04 23:42:03

+0

是的,没错! – 2013-03-05 04:35:03

相关问题