目标:
如果用户前两次键入“ right”,笑脸就会变得悲伤,因为它无法走出森林。
如果用户在第3次,第4次,第5次等上键入“ right”,笑脸就会变得沮丧,砍伐一些树木,制作桌子并将其翻转过来。
如果用户键入其他任何内容,则会向用户显示一条消息,提示“输入无效”。并要求再次输入。
如果用户键入“ left”,笑脸就会离开森林,程序终止。
Python代码:
n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
i = 1
while (n.lower() == "right"):
if i < 3:
n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
elif i >= 3:
n = input("You are in the Lost Forest\n****************\n****** ***\n (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
i = i + 1
while (n.lower() != "right") and (n.lower() != "left"):
n = input("Invalid Input\nYou are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
while (n.lower() == "left"):
print("\nYou got out of the Lost Forest!\n\o/")
break
错误:
如果用户前两次键入“ right”或“ left”以外的任何内容,然后键入“ right”,则程序将立即终止,而没有机会键入“ left”。
我应该如何编辑代码?
答案 0 :(得分:1)
更多类似内容:
lost = "You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? "
inp = lost
rightCount = 0
while True:
n = input(inp)
if (n.lower() == "right"):
rightCount = rightCount + 1
if rightCount > 3:
inp = ("You are in the Lost Forest\n****************\n****** ***\n (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
else:
inp = lost
elif n.lower() == "left":
print("\nYou got out of the Lost Forest!\n\o/") #syntax error fixed
break
else:
inp = ("Invalid Input\nYou are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
答案 1 :(得分:0)
我想您忘记了将所有内容包装到while True
循环(或其他无穷循环)中。
是您需要的吗?
while True:
n = input("You are in the Lost Forest\n****************\n****************\n :)\n****************\n****************\nGo left or right? ")
i = 1
if (n.lower() == "right"):
if i < 3:
n = input("You are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
elif i >= 3:
n = input("You are in the Lost Forest\n****************\n****** ***\n (╯°□°)╯︵ ┻━┻\n****************\n****************\nGo left or right? ")
i = i + 1
if (n.lower() != "right") and (n.lower() != "left"):
n = input("Invalid Input\nYou are in the Lost Forest\n****************\n****************\n :(\n****************\n****************\nGo left or right? ")
if (n.lower() == "left"):
print("\nYou got out of the Lost Forest!\n\o/")
break