Python编译器跳过“if”语句

2024-06-17 14:47:24 发布

您现在位置: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()                                                            

                          

Tags: orthetoinputifvaluefloatprint
2条回答

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

The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.

输出案例

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")

请注意,除非choice和choice2相同

if option2 != choice or option2 != choice2:

这永远是真的。选项2实际上并不等于这些选项之一。使用and而不是or可能是您想要的

相关问题 更多 >