问题描述
我需要帮助(新学生-2周).我想对这段代码进行尽可能最小的更改,以使用户有3次机会为程序中的每次转化输入错误的值.输入错误的值3次后,程序应终止.唯一的要求是代码必须包含一个FOR循环.我不知道它是否需要一个FOR循环或3个FOR循环(每次转换一个).我已经尝试了很多情况,但似乎无法解决问题.
谢谢 !!!!
I need assistance (new student - 2 weeks). I'd like to get the most minimal changes possible to this code that allows a user 3 chances at typing in the incorrect values for each conversion in the program. After typing in an incorrect value 3 times, the program should terminate. The only requirement is that the code must contain a FOR loop. I don't know if it requires one FOR loop or 3 FOR loops (one for each conversion). I've tried so many scenarios and can't seem to get it right.
Thank you !!!!
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')
我尝试过的一种情况,但不知道如何合并下一次转换或使转换正确.
One scenario I've tried but don't know how to incorporate the next conversion or get it just right.
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或中断.)我还必须在最内层的循环中插入一个count = 3,因为pgm仍然希望即使输入有效也可以运行3次.我知道这是使用While循环.但是有没有办法将其作为"For"循环来执行呢?还是不可能? (希望下面的对齐方式很好,因为我在记事本中对其进行了修改.)
Thank you ! I modified what you listed into the format that I would need based on what we've learned so far. (We haven't learned sys.exit or breaks yet.) I also had to insert a count = 3 in the inner most loop as the pgm still wanted to run 3 times even with valid input. I know this is using a While loop. But is there a way to still do this as a 'For' Loop? Or is that not possible? (Hopefully the alignment below is good as I modified it in notepad.)
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
推荐答案
最简单的方法可能是为每个提示循环,如下面的伪Python代码所示:
The easiest for you is probably to make a loop for each prompt like in the following pseudo Python code:
# 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)
在每个<...>
中,您都必须为程序编写一些有意义的内容.
at each <...>
you will have to write something sensible for your program.
更新
具有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)
在每个for
循环之前记住ok = False
.
更新2
这是我看到的整个程序,您在每个输入上都有3次机会.我已自由调整了您的print
语句.
This is the whole program as I see it, where you get 3 chances on each input. I have taken the liberty of adjusting your print
statements.
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')
这篇关于Python 3.5.1-给用户3次无效尝试,然后终止pgm(简单的FOR循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!