-1

我已经用 Python 编写了所有数学函数,但是当用户提示时无法让程序转到正确的选项

print ("""Calculations Menu:
    1) Area (Square)
    2) Area (Rectangle)
    3) Area (Circle)
    4) Perimeter (Square)
    5) Perimeter (Rectangle)
    6) Perimeter (Circle)
    7) Exit
""")

choice = input("Input Menu Choice (1,2,3,4,5,6 or 7)?\n")

if choice == "1":
    print ("You have chosen Area (Square)")

    def area_square (width, height):
        return width * height

    def positive_input (prompt):
        number = float(input(prompt))
        while number <=0:
            print ("Must be a positive number.")
            number = float(input(prompt))
        return number

    w = positive_input ("Width: ")
    h = positive_input ("Height: ")

    print ("Width = ", w, "Height = ", h, "So Area =", area_square(w, h))

if choice == "2":
    print ("You have chosen Area (Rectangle)")

    def area_rectangle (width, height):
        return width * height

    def positive_input (prompt):
        number = float(input(prompt))
        while number <=0:
            print ("Must be a positive number.")
            number = float(input(prompt))
        return number

    w = positive_input ("Width: ")
    h = positive_input ("Height: ")

    print ("Width = ", w, "Height = ", h, "So Area =", area_rectangle(w, h))

if choice == "3":
    print ("You have chosen Area (Circle)")

    def area_circle (radius, pi):
        return radius**2 * pi

    def positive_input (prompt):
        number = float(input(prompt))
        while number <=0:
            print ("Must be a positive number.")
            number = float(input(prompt))
        return number

    radius = positive_input ("radius: ")
    pi = 3.14159265

    print ("Radius = ", radius, "Pi = ", pi, "So Area =", area_circle(radius, pi))

if choice == "4":
    print ("You have chosen Perimeter (Square)")

    def perimeter_square (side, ):
        return side * 4

    def positive_input (prompt):
        number = float(input(prompt))
        while number <=0:
            print ("Must be a positive number.")
            number = float(input(prompt))
        return number

    side = positive_input ("side: ")

    print ("side = ", "So Perimeter =", perimeter_square(side,))

if choice == "5":
    print ("You have chosen Perimeter (Rectangle)")

    def perimeter_rectangle (sideA, sideB,):
        return (sideA + sideB) * 2

    def positive_input (prompt):
        number = float(input(prompt))
        while number <=0:
            print ("Must be a positive number.")
            number = float(input(prompt))
        return number

    sideA = positive_input ("Length: ")
    sideB = positive_input ("Width: ")

    print ("Length = ", sideA, "Width =  ", sideB, "So Perimeter =", perimeter_rectangle(sideA, sideB))

if choice == "6":
    print ("You have chosen Perimeter (Circle)")

    def perimeter_circle (diameter, pi):
        return diameter * pi

    def positive_input (prompt):
        number = float(input(prompt))
        while number <=0:
            print ("Must be a positive number.")
            number = float(input(prompt))
        return number

    diameter = positive_input ("Diameter: ")
    pi = 3.14159265

    print ("Diameter = ", diameter, "Pi =  ", pi, "So Perimeter =", perimeter_circle(diameter, pi))
4

2 回答 2

4

当我运行你的代码时,它工作得很好,当然它会经历所有 7 种可能性,而不仅仅是你选择的那种。

(请注意,如果您使用 Python 2.x 解释器而不是 3.x 运行此代码,它似乎可以工作,但从不做任何事情。这是因为在 2.x 中,input评估其输入choice,例如, int3而不是 string "3"。但是如果您按预期使用 Python 3 解释器运行它,那不是问题。)

它这样做的原因只是缩进。你的代码应该只针对给定的选择运行,但它没有缩进到正确的if块下。一旦你解决了这个问题,一切都很好。

因为定义完全相同的positive_input函数 6 次是愚蠢的,所以我把那部分移到了顶部。否则,我所做的唯一更改是缩进:

def positive_input (prompt):
    number = float(input(prompt))
    while number <=0:
        print ("Must be a positive number.")
        number = float(input(prompt))
    return number

print ("""Calculations Menu:
    1) Area (Square)
    2) Area (Rectangle)
    3) Area (Circle)
    4) Perimeter (Square)
    5) Perimeter (Rectangle)
    6) Perimeter (Circle)
    7) Exit
    """)

choice = input("Input Menu Choice (1,2,3,4,5,6 or 7)?\n")

if choice == "1":
    print ("You have chosen Area (Square)")

    def area_square (width, height):
        return width * height

    w = positive_input ("Width: ")
    h = positive_input ("Height: ")

    print ("Width = ", w, "Height = ", h, "So Area =", area_square(w, h))

if choice == "2":
    print ("You have chosen Area (Rectangle)")

    def area_rectangle (width, height):
        return width * height

    w = positive_input ("Width: ")
    h = positive_input ("Height: ")

    print ("Width = ", w, "Height = ", h, "So Area =", area_rectangle(w, h))

if choice == "3":
    print ("You have chosen Area (Circle)")

    def area_circle (radius, pi):
        return radius**2 * pi

    radius = positive_input ("radius: ")
    pi = 3.14159265

    print ("Radius = ", radius, "Pi = ", pi, "So Area =", area_circle(radius, pi))

if choice == "4":
    print ("You have chosen Perimeter (Square)")

    def perimeter_square (side, ):
        return side * 4

    side = positive_input ("side: ")

    print ("side = ", "So Perimeter =", perimeter_square(side,))

if choice == "5":
    print ("You have chosen Perimeter (Rectangle)")

    def perimeter_rectangle (sideA, sideB,):
        return (sideA + sideB) * 2

    sideA = positive_input ("Length: ")
    sideB = positive_input ("Width: ")

    print ("Length = ", sideA, "Width =  ", sideB, "So Perimeter =", perimeter_rectangle(sideA, sideB))

if choice == "6":
    print ("You have chosen Perimeter (Circle)")

    def perimeter_circle (diameter, pi):
        return diameter * pi

    diameter = positive_input ("Diameter: ")
    pi = 3.14159265

    print ("Diameter = ", diameter, "Pi =  ", pi, "So Perimeter =", perimeter_circle(diameter, pi))

当我们在做的时候,我可能会做一些其他的改变(大部分是次要的):

首先,Python 风格强烈反对在函数调用的括号前放置空格。

接下来,您可以使用为您的 Python 构建类型math.pi获得pi正确的精度float,而不必输入您自己的近似值并猜测精度。

如果您将一堆相互排斥的if语句链接在一起,那么 usingelif可以让读者清楚地知道它们应该是相互排斥的。

您可能需要考虑更进一步并将每个选项移动到一个函数中,因此您可以这样做:

if choice == "1":
    do_area_square()
elif choice == "2":
    do_area_rectangle()
# ...

… 甚至:

functions = {"1": do_area_square, "2": do_area_rectangle, # ...
functions[choice]()

… 或者:

functions = [do_area_square, do_area_rectangle, ...]
functions[int(choice)-1]()
于 2013-04-29T18:45:52.230 回答
1

也许这会对您的问题有所了解:

In [6]: choice = input("Input Menu Choice (1,2,3,4,5,6 or 7)?\n")
Input Menu Choice (1,2,3,4,5,6 or 7)?
1

In [7]: type(choice)
Out[7]: int

In [9]: choice == "1"
Out[9]: False

In [10]: choice == 1
Out[10]: True
于 2013-04-29T18:38:35.183 回答