繁体   English   中英

为什么不读这个'if'语句?

[英]Why isn't this 'if' statement being read?

在我的代码中,我希望两个答案'对立'和'斜边',有两个不同的结果,但是,每当我测试代码并回答时,'相反',它会忽略其余的代码并转到'斜边的问题。 我是否将其格式化错误/是否有更简单的方法来执行此操作/等等?

from math import *

    def  main():

       #Trignometry Problem

        def answer():
            answer = raw_input()

    while True:

        # Phrase Variables
        phrase1 = ("To begin, we will solve a trigonometry problem using sin.")
        phrase2 = ("Which is known - hypotenuse or opposite?")
        phrase3 = ("Good! Now, we will begin to solve the problem!")
        phrase4 = ("Please press any key to restart the program.")

        print phrase1
        origin=input("What is the origin?")
        print phrase2
        answer = raw_input()
        if answer == ("Hypotenuse.") or ("Hypotenuse") or ("hypotenuse") or ("hyotenuse."):
            hypotenuse=input("What is the hypotenuse?")
            print "So, the problem is " + "sin" + str(origin) + " = " + "x" + "/" + str(hypotenuse) + "?"
            answer = raw_input()
            if answer == ("Yes.") or ("yes") or ("yes.") or ("Yes"):
                print phrase2
            answer = raw_input()
            print phrase4
            answer = raw_input()
            if answer == ("No."):
                break 
        if answer == ("Opposite."):
            opposite=input("What is the opposite?")
            print "So, the problem is " + "sin" + str(origin) +  " = " + str(opposite) + "/" + "x" + "?"
            answer = raw_input()
            if answer == ("Yes.") or ("yes") or ("yes.") or ("Yes"):
                print phrase2
        answer = raw_input()
        print phrase4
        answer = raw_input()
        if answer == ("No."):
            break


    main()

简短的回答

你可能想要改变那些:

if answer == ("Hypotenuse") or ("Hypotenuse.") ...

这样:

if answer in ("Hypotenuse", "Hypotenuse.", ...):

说明

表达方式:

answer == ("Foo") or ("Bar")

评估如下:

(answer == ("Foo")) or (("Bar"))

"Bar"永远是True

显然,正如评论中所指出的, "HYPOTENUSE" in answer.upper()中的"HYPOTENUSE" in answer.upper()是最好的解决方案。

暂无
暂无

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

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