Python - 在while循环中使if语句重新启动循环,而不是条件?

时间:2016-10-26 22:46:06

标签: python python-3.x

我有代码可以确定形状的体积,由输入决定。我有几个if语句嵌套在while循环中。我希望循环在它要求输入的最开始重新启动,但它会被卡在条件语句中。例如,如果我输入“cube”,它会计算音量,然后再次要求我输入尺寸 - 而我希望它向我询问形状,然后再次通过if语句。最后,如果我输入的形状无效,则应该让我再次输入,如果输入有效,则相应地继续。但它没有,它陷入无限循环,永远不会停止要求我输入形状。

import math
import decimal

shape = str(input("Please enter a shape: "))
shape = shape.lower()

shapes = ["cube", "pyramid", "ellipsoid", "quit"]

stored_cube_vol = []
stored_pymd_vol = []
stored_ellip_vol = []

while shape != "quit":
    if shape == "cube":
        def cube_volume(cube_side_length):
            cube_total_vol = decimal.Decimal(cube_side_length**3)
            return round(cube_total_vol, 2)

        cube_side_length = float(input("Enter the length of length of one side of the cube: "))

        print("The volume of a cube with sides with a length of", cube_side_length, "is", cube_volume(cube_side_length))

        stored_cube_vol.append(cube_volume(cube_side_length))

    elif shape == "pyramid":
        def pymd_volume(pymd_base, pymd_height):
            pymd_total_vol = decimal.Decimal(pymd_base**2)*pymd_height*(1/3)
            return round(pymd_total_vol, 2)

        pymd_base = float(input("Enter the length of the base of the pyramid: "))
        pymd_height = float(input("Enter the height of the pyramid: "))

        print("The volume of a pyramid with a base of", pymd_base, "and a height of", pymd_height, "is", pymd_volume(pymd_base, pymd_height))

        stored_pymd_vol.append(pymd_volume(pymd_base, pymd_height))

    elif shape == "ellipsoid":
        def ellip_volume(ellip_rad_1, ellip_rad_2, ellip_rad_3):
            ellip_total_vol = decimal.Decimal(math.pi*(4/3)*ellip_rad_1*ellip_rad_2*ellip_rad_3)
            return round(ellip_total_vol, 2)

        ellip_rad_1 = float(input("Enter the first radius: "))
        ellip_rad_2 = float(input("Enter the second radius: "))
        ellip_rad_3 = float(input("Enter the third radius: "))

        print("The volume of an ellipsoid with radii", ellip_rad_1, ellip_rad_2, "and", ellip_rad_3, "is", ellip_volume(ellip_rad_1, ellip_rad_2, ellip_rad_3))

        stored_ellip_vol.append(ellip_volume(ellip_rad_1, ellip_rad_2, ellip_rad_3))

    elif shape == "quit":
        print("You have come to the end of the session.")
        print("The volumes calculated for each shape are shown below.")
        print("Cube: ", sorted(stored_cube_vol))
        print("Pyramid: ", sorted(stored_pymd_vol))
        print("Ellipsoid: ", sorted(stored_ellip_vol))

    else:
        str(input("Please enter a shape: ").lower())

2 个答案:

答案 0 :(得分:0)

您在shape =条款中遗漏了else

如果我理解正确,只需删除 else子句并离开:

shape = str(input("Please enter a shape: ")).lower()

在循环结束时。

只要他没有输入"quit",即使他确实输入了有效选项,也会询问用户。

答案 1 :(得分:0)

我对代码做了一些更改。我已将语句从用户输入到while循环中,并在'if =='quit''语句中添加了break语句。

改变1:

import math
import decimal

shapes = ["cube", "pyramid", "ellipsoid", "quit"]

stored_cube_vol = []
stored_pymd_vol = []
stored_ellip_vol = []
shape = " "
while shape != "quit":
    shape = str(input("Please enter a shape: "))
    shape = shape.lower()

改变2:

elif shape == "quit":
    print("You have come to the end of the session.")
    print("The volumes calculated for each shape are shown below.")
    print("Cube: ", sorted(stored_cube_vol))
    print("Pyramid: ", sorted(stored_pymd_vol))
    print("Ellipsoid: ", sorted(stored_ellip_vol))
    break