def cube(a,b,c):
s=2*(a*b+b*c+a*c)
v=a*b*c
return [s,v]
a=b=c=-1
while a<=0 or b<=0 or c<=0:
a,b,c=map(float,input('请输入长方体的长,宽,高(逗号分隔):').split(','))
s,v=cube(a,b,c)
print('边长为{},{},{}的长方体的表面积为:{},体积为:{}.'.format(a,b,c,s,v))
觉得有用的话采纳一下哈
def cube(a, b, c):
return 2 * (a * b + a * c + b * c), a * b * c
while 1:
x = input("长宽高:")
if x.count(",") > 2:
continue
try:
length, width, height = x.split(",")[0], x.split(",")[1], x.split(",")[2]
except IndexError:
continue
try:
if float(length) <= 0 or float(width) <= 0 or float(height) <= 0:
continue
except ValueError:
continue
print("边长:{0},{1},{2},表面积:{3},体积:{4}.".format(length, width, height,
cube(float(length), float(width), float(height))[0],
cube(float(length), float(width), float(height))[1]))
break