限制语句中的循环数?

时间:2018-10-03 18:54:32

标签: python loops while-loop

我有一个编程基础课的作业。在这种情况下,我必须在程序终止之前接受无效输入一定次数(在这种情况下为五次)。该书似乎没有解释在这种情况下限制循环运行的次数,因此我提供了代码作为指导。如所写,它继续循环并且不会停止。

max = 5
#Input miles to be converted
miles = float(input('Enter the number of miles to convert to kilometers: '))
milesToKms = miles*1.6 #miles to kilometers formula
for counter in range(max):
    if miles >= 0:
        print ('Miles converted: ', miles)
        print ('Kilometers: ', milesToKms) #display kilometers result
        break
    elif miles <0:
        while miles < 0:
            print ('Invalid value entered.')
            miles = float(input('Enter a valid number of miles to convert: '))
else:
    print ('Too many invalid entries submitted.')
    exit ()              

3 个答案:

答案 0 :(得分:2)

您在这里不需要或不需要611 09-010循环。您的外部while循环会自行限制尝试的次数,并重新检查每个循环的条件,因此它会自己完成所有工作。

我已经对其进行了一些清理,以删除多余的for代码,该代码还具有避免在最终尝试失败后避免提示输入的副作用,因为无论如何该循环都会停止:

input

答案 1 :(得分:0)

在给出5个错误输入之前一直进行计算的解决方案:

import sys

invalids = 0
maxWrongInputs = 5

# loop until too many wrongs were done
while invalids < maxWrongInputs:
    miles = float(input('Enter the number of miles to convert to kilometers:'))
    if miles >= 0:
        milesToKms = miles * 1.6 
        print('Miles converted:', miles)
        print('Kilometers:', milesToKms) 
    else:  
        # add another to your counter of invalid attempts
        invalids += 1
        print('Invalid value entered.')
else:
    sys.exit('Exited after {} invalid entries.'.format(invalids))

输入:3. 4. 5. 6. 7. 8. -1. -1. -1. 4. -1. -1.

输出:

Enter the number of miles to convert to kilometers:3
Miles converted: 3.0
Kilometers: 4.800000000000001
Enter the number of miles to convert to kilometers:4
Miles converted: 4.0
Kilometers: 6.4
Enter the number of miles to convert to kilometers:5
Miles converted: 5.0
Kilometers: 8.0
Enter the number of miles to convert to kilometers:6
Miles converted: 6.0
Kilometers: 9.600000000000001
Enter the number of miles to convert to kilometers:7
Miles converted: 7.0
Kilometers: 11.200000000000001
Enter the number of miles to convert to kilometers:8
Miles converted: 8.0
Kilometers: 12.8
Enter the number of miles to convert to kilometers:-1    # 1.
Invalid value entered.
Enter the number of miles to convert to kilometers:-1    # 2.
Invalid value entered.
Enter the number of miles to convert to kilometers:-1    # 3.
Invalid value entered.
Enter the number of miles to convert to kilometers:4
Miles converted: 4.0
Kilometers: 6.4
Enter the number of miles to convert to kilometers:-1    # 4.
Invalid value entered.
Enter the number of miles to convert to kilometers:-1    # 5.
Invalid value entered.
Exited after 5 invalid entries.

答案 2 :(得分:-1)

您的内部while循环引起了问题:

max = 5
#Input miles to be converted
for counter in range(max):
    miles = float(input('Enter the number of miles to convert to kilometers: '))
    milesToKms = miles*1.6 #miles to kilometers formula
    if miles >= 0:
        print ('Miles converted: ', miles)
        print ('Kilometers: ', milesToKms) #display kilometers result
        break
    elif miles <0:
        print ('Invalid value entered.')

if counter == (max - 1):
    print ('Too many invalid entries submitted.')
    exit ()

有效输出:

Enter the number of miles to convert to kilometers: 10
('Miles converted: ', 10.0)
('Kilometers: ', 16.0)

无效的输出:

Enter the number of miles to convert to kilometers: -1
Invalid value entered.
Enter the number of miles to convert to kilometers: -2
Invalid value entered.
Enter the number of miles to convert to kilometers: -3
Invalid value entered.
Enter the number of miles to convert to kilometers: -4
Invalid value entered.
Enter the number of miles to convert to kilometers: -5
Invalid value entered.
Too many invalid entries submitted.