我的Python 3.x程序出了什么问题?

时间:2013-04-29 18:30:56

标签: python python-3.x

我已经用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))

2 个答案:

答案 0 :(得分:4)

当我运行你的代码时,它运行得很好,当然除了它通过所有7种可能性而不仅仅是你选择的那种。

(请注意,如果您使用Python 2.x解释器而不是3.x运行此代码,它似乎可以工作,但从不执行任何操作。这是因为在2.x中,{{1} }评估它的输入,因此input将是例如int choice而不是字符串3。但是如果你按照预期使用Python 3解释器运行它,那么&#39;不是问题。)

它这样做的原因只是缩进。您已经获得了仅针对给定选项运行的代码,但它没有在正确的"3"块下缩进。一旦你解决了这个问题,一切都很好。

由于定义完全相同的if函数6次是愚蠢的,我将该部分移到了顶部。否则,我做的唯一改变就是缩进:

positive_input

虽然我们正在努力,但我可能会做出一些其他改变(主要是次要的):

首先,Python style强烈建议不要在函数调用的括号前加上空格。

接下来,您可以使用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版本math.pi类型获得pi的正确精度,而不必输入您自己的近似值并猜测精确度。

如果您将一堆相互排斥的float语句链接在一起,使用if会使读者清楚地知道他们应该互相排斥。

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

elif

......甚至:

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

......或:

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

答案 1 :(得分: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