我需要帮助(新生-2周)。我想得到这段代码可能的最微小的变化,允许用户3次在程序中输入错误的值。输入错误值3次后,程序应终止。唯一的要求是代码必须包含FOR循环。我不知道它是需要一个FOR循环还是3个FOR循环(每次转换一个)。我尝试了很多种方案,但似乎都没能把它做好。
谢谢您!!!!在miles = float(input('Type miles to be converted to km.\n'))
if miles >= 0:
milesToKm = miles * 1.6
print (miles, 'miles is', format(milesToKm, ',.1f'), 'kilometers.\n')
inch = float(input('Give me inches to convert to cm.\n'))
if inch >=0:
inchesToCm = inch * 2.54
print (inch, 'inches is', format(inchesToCm, '.2f'), 'centimeters.\n')
temp = float(input('Give me a Fahrenheit temp to convert to Celsius.\n'))
if temp <= 1000:
celsius = (temp - 32) * (5/9)
print (temp, 'degrees Fahrenheit is', format (celsius, '.1f'), 'Celsius.\n')
else:
print ('Wrong input, too high.\n')
else:
print ('Wrong input, no negatives.\n')
else:
print ('Wrong input, no negatives.\n')
我尝试过一个方案,但不知道如何合并下一个转换,或是让它恰到好处。在
^{pr2}$
谢谢你!我根据我们目前所学的知识,修改了你所列的格式。)我们还没学会系统出口我还不得不在最内层的循环中插入count=3,因为pgm仍然希望在有效输入下运行3次。我知道这是用While循环。但是,有没有一种方法仍然可以作为一个“For”循环来完成呢?还是不可能?(希望下面的对齐方式很好,因为我在记事本中修改了它。)count = 0
while count < 3:
miles = float(input('Type miles to be converted to km.\n'))
if miles >= 0:
milesToKm = miles * 1.6
print (miles, 'miles is', format(milesToKm, ',.1f'), 'kilometers.\n')
count = 0:
while count < 3:
inch = float(input('Give me inches to convert to cm.\n'))
if inch >=0:
inchesToCm = inch * 2.54
print (inch, 'inches is', format(inchesToCm, '.2f'), 'centimeters.\n')
count = 0:
while count < 3:
temp = float(input('Give me a Fahrenheit temp to convert to Celsius.\n'))
if temp <= 1000:
celsius = (temp - 32) * (5/9)
print (temp, 'degrees Fahrenheit is', format (celsius, '.1f'), 'Celsius.\n')
count = 3
else:
print ('Wrong input, too high.\n')
count+=1
else:
print ('Wrong input, no negatives.\n')
count +=1
else:
print ('Wrong input, no negatives.\n')
count +=1