使用 while 循环+if 制作一个简单的游戏,但输入线不起作用

发布于 2025-01-15 11:53:53 字数 831 浏览 3 评论 0原文

在学习了 while 循环和 if 后,我试图制作一个超级简单的文本游戏,但问题是:在输入行要求输入右/左后,无论我输入左还是右,代码都无法工作。我将“选择”变量更改为左侧,然后它就可以正常工作了...我不知道这里有什么问题...请告诉我如何解决这个问题以使其正常工作。

choice=str(input("you are now in a forest, please pick a direction(left or right)"))
while choice == "right":
    print("a strange wind had sent you back to forest")
    choice=str(input("you are now in a forest, please pick a direction(left or right)"))
if choice == "left":
    print("u saw a cave, in or not?")
    choice= str(input("type in or not "))
    if choice == "in" :
        print("u saw a bear claw and that's the last view in your eyes")
    elif choice == "not":
        print("after two and a half hours walking, you are now out of the forest")

这就是代码,我认为输入行有一些问题,但我不知道确切的问题是什么。当我更改为 choice="right" 或 choice="left" 时,代码可以正常工作,但对于此输入行,它不能...

after learned while loop and if, i m trying to make a super simple text game, but here is the problem: after the input line asked to type right/left, and no matter i type left or right, the code wont work. i changed the "choice" variable to left then it's working properly... I dont know what's the problems here...please tell me how to fix this to make it work.

choice=str(input("you are now in a forest, please pick a direction(left or right)"))
while choice == "right":
    print("a strange wind had sent you back to forest")
    choice=str(input("you are now in a forest, please pick a direction(left or right)"))
if choice == "left":
    print("u saw a cave, in or not?")
    choice= str(input("type in or not "))
    if choice == "in" :
        print("u saw a bear claw and that's the last view in your eyes")
    elif choice == "not":
        print("after two and a half hours walking, you are now out of the forest")

that's the code, I think the input line got some problems but I don't know what exact problems are. when I change to choice="right" or choice="left", the code is working but with this input line it's not...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢烬 2025-01-22 11:53:53

这段代码对我有用;您是否使用全小写的 leftright 且不带引号或其他字符?您可能需要在 input 调用末尾添加 .lower().strip() 来将所有内容小写并删除前导/尾随空格。

This code is working for me; are you using all lowercase left or right with no quotes or other characters? You may want to add a .lower().strip() to the end of your input call to lowercase everything and remove leading/trailing whitespace.

如歌彻婉言 2025-01-22 11:53:53

对不起大家,我刚刚发现原因!
我习惯在单词之间输入空格,使句子清晰,但在这种情况下,我输入空格+左/右,而 while 循环或 if 循环只查找“左”或“右”,所以我给了他们一个输入他们认不出……那是愚蠢的……
我很抱歉
谢谢大家的帮助!

sorry guys I just found out why!
I have the habit to type a space between words which makes sentences clear but in this case, i type space+left/right and the while loop or if loop is looking for only "left' or "right" so I gave them a input they cannot recognize... that's stupid...
I am so sorry
thank you all for the help tho!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文