3 回答
TA贡献1854条经验 获得超8个赞
请注意:如果这是作业请不要在没有理解一切的情况下盲目复制代码,我会尽力通过#注释解释代码
import math # required to use ceil() function , ex: ceil(3.2) = 4
def required_length(a, b): # functions make your code organized
length = max(a,b)
width = min(a,b)
print('length = ', length)
print('width = ', width)
print('Total length required lengthways = ', math.ceil(length))
print('Total length required widthways = ', math.ceil(width))
print() # to look nice print empty line for getting new input
while True: # loop forever
a = float(input('enter room dimension 1 (m): '))
b = float(input('enter room dimension 2 (m): '))
if a <= 0 or b <= 0: # exit loop if user entered zero or minus
print('invalid dimensions')
break
required_length(a, b) # call our function
TA贡献1776条经验 获得超12个赞
刚刚写了一个优化代码
sentinal = True
while sentinal:
d1 = float(input("enter room dimension1 (m): "))
d2 = float(input("enter room dimension2 (m): "))
if 0 in (d1,d2) or 0.0 in (d1,d2):
sentinal=False
else:
length, width = (d1,d2) if d1>d2 else (d2,d1)
print("leangth = %.3f m" %(length))
print("width = %.3f m" %(width))
TA贡献1798条经验 获得超7个赞
sentinal = 0
d1 = 1.0
d2 = 1.0
while d1 > sentinal and d2 > sentinal:
d1 = float(input("enter room dimension1 (m):"))
d2 = float(input("enter room dimension2 (m):"))
if d1 > d2:
length = d1
width = d2
print("length = %.3f m" %(length))
print("width = %.3f m" %(width))
print("Total length required lengthways: ", int(length)+1))
print("Total length required widthways: ", int(width)+1))
else:
length = d2
width = d1
print("length = %.3f m" %(length))
print("width = %.3f m"%(width))
print("Total length required lengthways: ", int(length)+1))
print("Total length required widthways: ", int(width)+1))
添加回答
举报