我需要帮助(新学生-2周)。我想对此代码进行最小的更改,以使用户3机会输入程序中每个转换的不正确值。输入不正确的值3次后,该程序应终止。唯一的要求是代码必须包含一个循环。我不知道它是需要一个循环还是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')
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')
我尝试过的一种情况,但不知道如何合并下一个转换或正确地进行。
count = 0
max = 1
for count in range (0, max):
miles = float (input('Type miles to convert to kilometers?n'))
if miles >=0:
max = 1
milesToKm = miles * 1.6
print (miles, 'miles is', format(milesToKm, ',.1f'), 'kilometers.n')
inch = float(input('Give me inches to convert to cm.n'))
else:
if max < 3:
max = max + 1
print ('Please use a non-negative number, try again.')
谢谢!我根据到目前为止所学到的知识修改了您将您列为所需的格式。(我们尚未学会sys.exit或breaks。)我还必须在最内部循环中插入一个计数= 3,因为PGM仍然希望运行3次,即使有有效的输入。我知道这使用了一个时循环。但是,是否有办法将其作为" 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
最简单的您可能是为每个提示符循环,例如以下pseudo python代码:
# Beginning of prompt
i = 0
while i < 3:
result = float(input(<question>))
if <isok>:
print(<result>)
break
else:
print(<error>)
i += 1
# Three failed inputs.
if i == 3:
print('Bye, bye!')
sys.exit(1)
在每个<...>
上,您必须为您的程序写一些明智的东西。
update
for
循环的变体:
# Beginning of prompt
ok = False
for i in range(3):
result = float(input(<question>))
if <isok>:
print(<result>)
ok = True
break
else:
print(<error>)
# Three failed inputs.
if not ok:
print('Bye, bye!')
sys.exit(1)
请记住ok = False
每个for
循环之前。
更新2
这是我看到的整个程序,每个输入都可以在其中获得3个机会。我已经自由调整您的print
语句。
import sys
# Beginning of prompt
ok = False
for i in range(3):
miles = float(input('Type miles to be converted to km.n'))
if miles >= 0:
milesToKm = miles * 1.6
print('{} miles is {:.1f} kilometers.'.format(miles, milesToKm))
ok = True
break
else:
print('Wrong input, no negatives.')
# Three failed inputs.
if not ok:
print('Bye, bye!')
sys.exit(1)
# Beginning of prompt
ok = False
for i in range(3):
inch = float(input('Give me inches to convert to cm.n'))
if inch >= 0:
inchesToCm = inch * 2.54
print('{} inches is {:.2f} centimeters.'.format(inch, inchesToCm))
ok = True
break
else:
print('Wrong input, no negatives.')
# Three failed inputs.
if not ok:
print('Bye, bye!')
sys.exit(1)
# Beginning of prompt
ok = False
for i in range(3):
temp = float(input('Give me a Fahrenheit temp to convert to Celsius.n'))
if temp <= 1000:
celsius = (temp - 32) * (5 / 9)
print('{} degrees Fahrenheit is {:.1f} Celsius.'.format(temp, celsius))
ok = True
break
else:
print('Wrong input, too high.')
# Three failed inputs.
if not ok:
print('Bye, bye!')
sys.exit(1)
print('All OK')