gpt4 book ai didi

python - 无法重复我的代码中要求的内容

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:23 25 4
gpt4 key购买 nike

我正在使用 Python 3.4.3 创建一个基于文本的冒险游戏,但我不知道如何让代码重复一个问题。在此之前有一堆旁白,如果这有助于理解正在发生的事情的话。

print("\n\n\n\n\nYou awake to find yourself in the center of a clearing in a forest.")
print("You stand up and decide to take a look around.")

str = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ")
print(" ")
if str in ("d"):
print("You see your combat boots and the grassy ground below your feet. ")

if str in ("l"):
print("The forest trees grow thicker and darker that way. You stare into the shadows and feel... cold...")

if str in ("r"):
print("The forest is warm and inviting that way, you think you can hear a distant birds chirp.")

if str in ("u"):
print("The blue sky looks gorgeous, a crow flies overhead... that's not a crow...")
print("It's a Nevermore, an aerial Grim. You stand still until it passes.")

if str in ("b"):
print("the grass slowly grows to dirt as the area falls into a mountain cliff. You now know where you are.")
print("Mount Glenn, one of the most Grim-infested places in all of Remnant.")
print("It's a bit unsettling.")

else:
print("Try that again")

我希望代码向用户重复问题,直到他们回答了所有可能的答案并继续下一个问题。我还希望它在他们得到其他答案时重复这个问题。我该怎么做呢?

最佳答案

不要使用 str 作为变量名,它会隐藏一个重要的内置函数并导致奇怪的问题。

使用 while 循环将输出限制为有效选项。

valid_choices = ('d', 'l', 'r', 'u', 'b',)

choice = None
while choice not in valid_choices:
text = input("Which direction do you steer your head? d= down, l= left, r= right, u= up, b= behind you: ")
choice = text.strip()

if choice == 'd':
print ('...')
elif choice == 'u':
print ('...')

另见:

关于python - 无法重复我的代码中要求的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36154927/

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