繁体   English   中英

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

[英]Python - Make if statement in while loops restart the loop, not the condition?

我有应该用来确定形状体积(由输入确定)的代码。 我有几个if语句嵌套在while循环中。 我希望循环在请求输入的一开始就重新开始,但是相反,它陷入了条件语句中。 例如,如果我输入“多维数据集”,它将计算体积,然后再次要求我输入尺寸-相反,我希望它询问形状,然后再次执行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())

您在else子句中缺少shape =

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

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

在循环的末尾。

只要用户没有输入"quit" ,这将询问用户,即使他确实输入了有效的选择。

我对代码做了一些更改。 我已将接受用户输入的语句放入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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM