我需要幫助(新生-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