如何解决 while 循环不取 0 或小于它的值的情况?

我无法计算并且 while 循环中的哨兵值等于或小于 0 所需的总长度纵向 = 6 m 所需的总长度横向 = 5 m

http://img3.mukewang.com/61bc3415000174d907460800.jpg

程序能够满足length和bread等于0的条件存在循环但不存在小于0的负数


我无法计算总长度并将其设为整数。


length = float    

width = float    

sentinal = 0    

d1 = 1


d2 = 1

for i in range(20):


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


        elif d2 > d1:


                length = d2


                width = d1


                print("length = %.3f m" %(length))


                print("width = %.3f m"%(width))


红颜莎娜
浏览 185回答 3
3回答

哔哔one

请注意:如果这是作业请不要在没有理解一切的情况下盲目复制代码,我会尽力通过#注释解释代码import math&nbsp; # required to use ceil() function , ex: ceil(3.2) = 4def required_length(a, b):&nbsp; # functions make your code organized&nbsp; &nbsp; length = max(a,b)&nbsp; &nbsp; width = min(a,b)&nbsp; &nbsp; print('length = ', length)&nbsp; &nbsp; print('width = ', width)&nbsp; &nbsp; print('Total length required lengthways = ', math.ceil(length))&nbsp; &nbsp; print('Total length required widthways = ', math.ceil(width))&nbsp; &nbsp; print()&nbsp; # to look nice&nbsp; print empty line for getting new inputwhile True:&nbsp; # loop forever&nbsp; &nbsp; a = float(input('enter room dimension 1 (m): '))&nbsp; &nbsp; b = float(input('enter room dimension 2 (m): '))&nbsp; &nbsp; if a <= 0 or b <= 0:&nbsp; # exit loop if user entered zero or minus&nbsp; &nbsp; &nbsp; &nbsp; print('invalid dimensions')&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; required_length(a, b)&nbsp; # call our function

叮当猫咪

刚刚写了一个优化代码sentinal = True&nbsp; &nbsp;&nbsp;while sentinal:&nbsp; &nbsp; &nbsp; &nbsp; d1 = float(input("enter room dimension1 (m): "))&nbsp; &nbsp; &nbsp; &nbsp; d2 = float(input("enter room dimension2 (m): "))&nbsp; &nbsp; &nbsp; &nbsp; if 0 in (d1,d2) or 0.0 in (d1,d2):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sentinal=False&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; length, width = (d1,d2) if d1>d2 else&nbsp; (d2,d1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("leangth = %.3f m" %(length))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("width = %.3f m" %(width))

元芳怎么了

&nbsp; &nbsp;sentinal = 0&nbsp; &nbsp; d1 = 1.0&nbsp; &nbsp; d2 = 1.0&nbsp; &nbsp; while d1 > sentinal and d2 > sentinal:&nbsp; &nbsp; &nbsp; &nbsp; d1 = float(input("enter room dimension1 (m):"))&nbsp; &nbsp; &nbsp; &nbsp; d2 = float(input("enter room dimension2 (m):"))&nbsp; &nbsp; &nbsp; &nbsp; if d1 > d2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; length = d1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width = d2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("length = %.3f m" %(length))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("width = %.3f m" %(width))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Total length required lengthways: ", int(length)+1))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Total length required widthways: ", int(width)+1))&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; length = d2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width = d1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("length = %.3f m" %(length))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("width = %.3f m"%(width))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Total length required lengthways: ", int(length)+1))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Total length required widthways: ", int(width)+1))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python