繁体   English   中英

Python if-else 语句的问题

[英]Problems with a Python if-else statement

我正在为 IT 课程做作业。 非常基础的 Python 项目。 我没有这方面的经验,但大部分时间都能掌握正在发生的事情。 该项目涉及做出涉及随机整数选择的选择。 我可以获得以 if-else 格式按照我想要的方式工作的选项,但 else 命令没有按照我想要的方式响应。 选项都正确加载,但如果由于某种原因选择为 1-3,程序将在打印“if”语句后打印“else”语句。 如果他们选择选项 4 或选择无效数字(else 语句的原因),则不会发生这种情况。

我在程序的前一部分中没有这个问题。 我一定错过了什么,但我不知道它是什么。 我应该重申,我是这方面的初学者,基本上是按照作业指示复制粘贴代码,然后对其进行编辑以满足我的需要。

interactions = ["Back Away Slowly","Point Towards the Chamber","Attack","Try To Communicate",]
import random
badchoice = random.randint(1,3)
loop = 1

while loop == 1:
    choice=menu(interactions, "How do you decide to interact?")
    print("")

    if choice == 1:
        print("You start to slowly back away from the man.")
        print("You worry you are giving off the wrong attitude.")
        print("")

        if choice == badchoice:
            loop=0
            print("The stranger was already weary of your presence.")
            print(interactions[choice-1], "was not the correct decision.")
            print("He calls for help. Villagers and guards immediately surround you.")
            print("You are thrown back into the chamber. Hitting your head, and getting knocked unconscious in the process.")
            print("You are back where you started and the items have been removed.")
            print("There is no escape.")
            print("Lamashtu's Curse can not be broken.")
            print("Game Over.")

        else:
            print("The stranger looks at you in confusion.")
            print("")

# Choices 2 and 3 go here. Same code. Seemed redundant to post all of it.    


    if choice == 4:
        loop=0
        print("The stranger is weary of your presence, and can not understand you.")
        print("You can see in his eyes that he wants to help,")
        print("and he escorts you back to his home for the time being.")
        print("He offers you some much needed food and water.")
        print("You are no closer to understanding what curse brought you here.")
        print("Perhaps tomorrow you will have more time to inspect your surroundings.")
        print("In time you may be able to communicate enough  with your host to explain your dilemma,")
        print("but for now you are trapped 6000 years in the past with no other options.")
        print("At least you've made it out alive thus far......")
        print("To be continued...")

    else:
        print("Please choose a number between 1 and 4.")

你的逻辑不对。 您最后的if语句将选择4指向它的True分支,并将其他所有内容指向False分支。 Python 完全按照您的要求执行。 没有对先前块的逻辑引用。

你需要的逻辑是

if choice == 1:
    ...
elif choice == 2:
    ...
elif choice == 3:
    ...
elif choice == 4:
    ...
else:
    print("Please choose a number between 1 and 4.")

else仅适用于同一级别的最新if ,即数字 4 的if 。其他三个if语句都是独立的。 因此,在检查完每一个之后,它会检查选择是否为 4,如果不是,则运行 else。

您需要将所有这些都作为同一语句的一部分,您可以通过对第一个之后的每个 if 使用elif来做到这一点。 像这样:

if choice == 1:
    ...
elif choice == 2:
    ...
elif choice == 3:
    ...
elif choice == 4:
    ...
else:
    ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM