gpt4 book ai didi

python - 如何解决 while 循环不采用 0 或小于它的值的情况?

转载 作者:行者123 更新时间:2023-12-01 08:09:10 26 4
gpt4 key购买 nike

我无法在 while 循环中计算 0 或更少的哨兵值所需的纵向总长度 = 6 m所需宽度总长度 = 5 m

Problem Statment

程序能够满足长度和面包等于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))

最佳答案

请注意:如果这是作业请不要在没有理解所有内容的情况下盲目复制代码,我会尽力通过#注释来解释代码

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

关于python - 如何解决 while 循环不采用 0 或小于它的值的情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55368592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com