Python 编译器跳过“if”语句

时间:2021-04-24 13:42:44

标签: python

我还是 Python 语言的新手,我的简单程序需要帮助。我的程序起初似乎工作得很好,但后来,python 似乎在“option1”和“option2”之后跳过了我的“if”语句,并且有点直接进入其无效错误,然后退出,仍然是新的,我需要帮助这个或者如果有人可以提出最佳选择,我将不胜感激。顺便说一下,我使用的是 python 3.9。

这是我的程序:

redo = True
choice = "a" or "A"
choice2 = "b" or "B"


while redo == True:
    # Program Interface [ variables are written specifically to avoid errors.]
    print("=================== GEOMETRIC CALCULATOR ===================")
    print("                         *SHAPE*")
    print("(a) Cube")
    print("(b) Cylender")
    print("------------------------------------------------------------")
    option1 = (input("Please choose a shape (a/b): "))  # User input
    if option1 != choice or option1 != choice2:
        print("Invalid selection, please only choose the given selection")
        exit()
    else:
        print("An error has occurred.")
    print("------------------------------------------------------------")
    print("                      *OPEARATIONS*")
    print("(a) Volume")
    print("(b) Surface Area")
    print("------------------------------------------------------------")
    option2 = input("Please choose an operation (a/b): ")  # User input
    if option2 != choice or option2 != choice2:
        print("Invalid selection, please only choose the given selection")
        exit()
    else:
        print("An error has occurred.")
        # Defining Functions
    def cubeVolume(a):
        v = float(a * a * a)  # Volume (Cube)
        return v
    def cubeSurfaceArea(a):
        sa = float(6 * (a * a))  # Surface Area (Cube)
        return sa
    def cylinderVolume(r, h):
        cv = float(3.1415 * (r * r) * h)  # Cylinder Volume
        return cv
    def cylinderSurfaceArea(r, h):
        cSA = (2 * 3.1415 * r * h) + (2 * 3.1415 * (r * r))  # Cylinder Surface Area
        return cSA
    # result selection program [ variables are written specifically to avoid errors.]
    try:
        if (option1 == "a" or option1 == "A") and (option2 == "a" or option2 == "A"):
            sidevalue1 = float(input("Enter side value: "))
            print("The volume of the cube is " + str(cubeVolume(sidevalue1)))
        elif (option1 == "a" or option1 == "A") and (option2 == "b" or option2 == "B"):
            sidevalue2 = float(input("Enter side value: "))
            print("The surface area of the cube is " + str(cubeSurfaceArea(sidevalue2)))
        elif (option1 == "b" or option1 == "B") and (option2 == "a" or option2 == "A"):
            radius1 = float(input("Enter radius value: "))
            height1 = float(input("Enter height value: "))
            print("The volume of the cylinder is " + str(cylinderVolume(radius1, height1)))
        elif (option1 == "b" or option1 == "B") and (option2 == "b" or option2 == "B"):
            radius2 = float(input("Enter radius value: "))
            height2 = float(input("Enter height value: "))
            print("The surface area of the cylinder is " + str(cylinderSurfaceArea(radius2, height2)))
    except:
        print("Invalid! Please input only a numerical value.")
        exit()
    # Asking the to try again
    reconf = input("Would you like to try again? (y/n): ")
    if reconf == "y" or reconf == "Y":
        redo = True
    elif reconf == "n" or reconf == "N":
        redo = False
    else:
        print("Error! Choose only from the given options.")
        exit()                                                            

                          

2 个答案:

答案 0 :(得分:0)

注意,除非choice和choice2相同,

if option2 != choice or option2 != choice2:

总是正确的。 option2 确实将不等于其中之一。使用 and 而不是 or 可能正是您想要的。

答案 1 :(得分:0)

if option1 != choice or option1 != choice2 对于 option1 的任何值都为真。选项 2 也是如此。

您可以检查一下:

option1 = input("Please choose a shape (a/b): ")  # User input
if option1 != choice or option1 != choice2:
   print(option1 != choice or option1 != choice2)  # x or y
   # print("x:", option1 != choice)
   # print("y:", option1 != choice2)
   print("Invalid selection, please only choose the given selection")

来自docs

<块引用>

表达式 x or y 首先计算 x;如果 x 为真,则其值 被退回;否则,计算 y 并且结果值为 返回。

输出案例

x 为假,y 为真

Please choose a shape (a/b): a
True
Invalid selection, please only choose the given selection

x 为真,y 为假

Please choose a shape (a/b): b
True
Invalid selection, please only choose the given selection

x 为真,y 为真

Please choose a shape (a/b): c
True
Invalid selection, please only choose the given selection

解决方案

您可以将有效选项组织到一个列表中...

choices = ["a", "A", "b", "B"]

并验证用户输入,如下所示:

option1 = input("Please choose a shape (a/b): ")  # User input
if option1 not in choices:
    print("Invalid selection, please only choose the given selection")