不断循环的python代码,所以它不会结束

时间:2018-11-25 22:23:22

标签: python loops

我试图做到这一点,即使在将输入提供给“输入命令”之后,代码也会在显示最后一个输入内容之后使我回到提供输入的选项。例如,我希望它在先前完成后连续显示“输入命令”提示。 有没有一种方法可以将代码循环回去而又不会结束?

c1 = "p"
c2 = "d"
c3 = "t"

command = ""
while command != c1:
    print("Enter a command: ")
    print ("(P)profile / (D)data ")
    command = input()
    if command == c1 or command == c2:
        print ("loading...")

        time.sleep(3)

        if command == c1:
            print ("User: Student")
        if command == c2:
            print ("ID: 111111111")

        time.sleep(1)  

    print ("Enter a command: ")
    print ("(P)profile / (D)data ")
    command = input()

1 个答案:

答案 0 :(得分:1)

将所有内容放入while True循环中,并删除要打印并要求输入的最后3行(因为您已经在开始时要求输入) 这应该可行:

c1 = "p"
c2 = "d"
c3 = "t"

while True:
    print("Enter a command: ")
    print ("(P)profile / (D)data ")

    command = input()
    if command == c1 or command == c2:
        print ("loading...")

        time.sleep(3)

        if command == c1:
            print ("User: Student")
        if command == c2:
            print ("ID: 111111111")

        time.sleep(1)