我需要帮助(新生-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 <&#61; 1000:
celsius &#61; (temp - 32) * (5/9)
print (temp, &#39;degrees Fahrenheit is&#39;, format (celsius, &#39;.1f&#39;), &#39;Celsius.\n&#39;)
else:
print (&#39;Wrong input, too high.\n&#39;)
else:
print (&#39;Wrong input, no negatives.\n&#39;)
else:
print (&#39;Wrong input, no negatives.\n&#39;)
我尝试过一个方案&#xff0c;但不知道如何合并下一个转换&#xff0c;或是让它恰到好处。在
^{pr2}$
谢谢你&#xff01;我根据我们目前所学的知识&#xff0c;修改了你所列的格式。)我们还没学会系统出口我还不得不在最内层的循环中插入count&#61;3&#xff0c;因为pgm仍然希望在有效输入下运行3次。我知道这是用While循环。但是&#xff0c;有没有一种方法仍然可以作为一个“For”循环来完成呢&#xff1f;还是不可能&#xff1f;(希望下面的对齐方式很好&#xff0c;因为我在记事本中修改了它。)count &#61; 0
while count <3:
miles &#61; float(input(&#39;Type miles to be converted to km.\n&#39;))
if miles >&#61; 0:
milesToKm &#61; miles * 1.6
print (miles, &#39;miles is&#39;, format(milesToKm, &#39;,.1f&#39;), &#39;kilometers.\n&#39;)
count &#61; 0:
while count <3:
inch &#61; float(input(&#39;Give me inches to convert to cm.\n&#39;))
if inch >&#61;0:
inchesToCm &#61; inch * 2.54
print (inch, &#39;inches is&#39;, format(inchesToCm, &#39;.2f&#39;), &#39;centimeters.\n&#39;)
count &#61; 0:
while count <3:
temp &#61; float(input(&#39;Give me a Fahrenheit temp to convert to Celsius.\n&#39;))
if temp <&#61; 1000:
celsius &#61; (temp - 32) * (5/9)
print (temp, &#39;degrees Fahrenheit is&#39;, format (celsius, &#39;.1f&#39;), &#39;Celsius.\n&#39;)
count &#61; 3
else:
print (&#39;Wrong input, too high.\n&#39;)
count&#43;&#61;1
else:
print (&#39;Wrong input, no negatives.\n&#39;)
count &#43;&#61;1
else:
print (&#39;Wrong input, no negatives.\n&#39;)
count &#43;&#61;1