不能重复我的化学需氧量

2024-03-29 10:36:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python3.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")

我希望代码向用户重复这个问题,直到他们回答了所有可能的答案,然后继续下一个问题。我还想让它在他们得到其他东西时重复这个问题。我该怎么做?你知道吗


Tags: andoftheto代码inyouyour
3条回答

不要使用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 ('...')

另请参见:

基本上,你可以把你的问题放在一个循环中,反复循环,直到你输入一个想要的“如果”案例。我修改了你的代码如下。请看一看

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.")

while True:
    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. ")
        break

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

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

    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.")
        break

    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.")
        break

    else:
        print("Try that again")

这样做:

answered = False
while not answered:
    str = input("Question")
    if str == "Desired answer":
        answered = True

相关问题 更多 >