gpt4 book ai didi

python - 不断循环python代码,这样就不会结束

转载 作者:行者123 更新时间:2023-12-01 08:41:25 25 4
gpt4 key购买 nike

我试图做到这一点,以便即使在我向“输入命令”提供输入后,代码也会在显示最后一个输入是什么后将我带回到提供输入的选项。例如,我希望它在之前完成后不断显示“输入命令”提示。有没有办法在不结束的情况下循环代码?

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()

最佳答案

将所有内容放入 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)

关于python - 不断循环python代码,这样就不会结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53472609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com