If和elif语句被忽略。直接进入else语句Python

时间:2020-06-18 10:50:29

标签: python if-statement

我正在尝试进行一个非常基本的迷你测验,但是我一直遇到一个问题,其中只有我的else语句得到了满足,我不确定为什么?

        userSleep = [input("Please enter 1 or 2 to answer the question. ")]
        if userSleep == 2:
                print("Thank you for your answer")
                time.sleep(1)
                print("What would you say stops you from getting more sleep?")
                print("[1.] Social Media")
                time.sleep(.25)
                print("[2.] On-demand TV services (i.e Netflix, Crunchyroll, YouTube etc.)")
                time.sleep(.25)
                print("[3.] Medically diagnosed insomnia")
                time.sleep(.25)
                print("[4.] School assignments/coursework")
                time.sleep(.25)
                print("[5.] Other")
                time.sleep(.5)
                userHabits = [input("Please enter a number corresponding one of the options above to answer the question. ")]
                print("Thank you for your answer")
                time.sleep(1)
                break
                clear()
        elif userSleep == 1:
                print("Thank you for your answer")
                time.sleep(1)
                break
                clear()
        else:
                print("Please input either a 1 or 2")```

I'm using Python 2.7

1 个答案:

答案 0 :(得分:0)

userSleep = [input("Please enter 1 or 2 to answer the question. ")]更改为userSleep = int(input("Please enter 1 or 2 to answer the question. "))