python的if else 语句使得循环停止的_Python Make if语句在while循环中重新启动循环,而不是条件?...

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值