如何限制函数中循环的迭代次数?

时间:2018-10-11 01:43:32

标签: python function loops

由于我的代码当前正在运行,因此对许多不正确条目的限制无法正常运行。

我已经尝试过不间断地运行它,从而正确地限制了迭代次数,但是随后似乎无法识别输入无效的输入后是否给出了有效的输入(它继续循环显示“输入有效的输入数字”。

按现在的顺序进行操作,无效的条目将不受限制。粗略的搜索并没有为我提供答案,我希望您能得到有关如何改进代码功能的指导,以便我可以使用这些限制(最多3个错误的输入,然后在第4个退出)。

在将其拆分为功能之前,它可以正常工作。

max = 3
#conversion factors
MILES_TO_KM = 1.6
GALLONS_TO_LITERS = 3.9
POUNDS_TO_KG = .45
INCHES_TO_CM = 2.54

#main conversion function
def main():
    miles = int(input('Enter the number of miles: '))
    miles_to_kms(miles)
    fahrenheit = int(input('Enter the degrees in Fahrenheit: '))
    fahrenheit_to_celsius(fahrenheit)
    gallons = int(input('Enter the number of gallons: '))
    gallons_to_liters(gallons)
    pounds = int(input('Enter the number of pounds: '))
    pounds_to_kgs(pounds)
    inches = int(input('Enter the number of inches: '))
    inches_to_cms(inches)

#define conversion for miles
def miles_to_kms(miles):
    while miles <0:
        for counter in range(4):
            miles = int(input('Enter a valid number of miles: '))
            break
        else:
            print ('Too many invalid entries submitted.')
            exit()
            #I have an issue here (and in the others) where after one invalid entry is given it loops in invalid entries.
    kilometers = miles * MILES_TO_KM
    print(miles, 'mile(s) is equal to', kilometers, 'kilometers.')

#define conversion for fahrenheit
def fahrenheit_to_celsius(fahrenheit):
    while fahrenheit < 0 or fahrenheit >1000:
        for counter in range(max+1):
            fahrenheit = int(input('Enter a valid temperature: '))
            break
        else:
            print ('Too many invalid entries submitted.')
            exit()
    celsius = ((fahrenheit-32)*5/9)
    print(fahrenheit, 'degree(s) Fahrenheit is equal to', celsius, 'degree(s) Celsius.')

#define conversion for gallons
def gallons_to_liters(gallons):
    while gallons <0:
        for counter in range(max+1):
            gallons = int(input('Enter a valid number of gallons: '))
            break
        else:
            print ('Too many invalid entries submitted.')
            exit()
    liters = gallons * GALLONS_TO_LITERS
    print(gallons, 'gallon(s) is equal to', liters, 'liter(s).')

#define conversion for pounds
def pounds_to_kgs(pounds):
    while pounds <0:
        for counter in range(max+1):
            pounds = int(input('Enter a valid number of pounds: '))
            break
        else:
            print ('Too many invalid entries submitted.')
            exit()
    kilograms = pounds * POUNDS_TO_KG
    print(pounds, 'pounds is equal to', kilograms, 'kilograms.')

#define conversion for inches
def inches_to_cms(inches):
    while inches <0:
        for counter in range(max+1):
            inches = int(input('Enter a valid number of inches: '))
            break
        else:
            print ('Too many invalid entries submitted.')
            exit()
    centimeters = inches * INCHES_TO_CM
    print(inches, 'inch(es) is equal to', centimeters, 'centimeter(s).')

main()

1 个答案:

答案 0 :(得分:0)

您立即在第一个循环中中断,所以我认为这不是您想要的。

然后,只要输入无效值,您的while循环就无限了

尝试改用此结构。基本上,与for-else(仅特定于Python的

相比),大多数情况下try-except更容易理解
unit = "miles"
conversion_rate = MILES_TO_KM
input_limit = 4 

units = None
for _ in range(input_limit):
    try:
        units = int(input('Enter a valid number of ' + unit +': '))
        if units < 0:
            continue # can't be negative 
        break # exit the loop if input can be made an integer 
    except ValueError:
        continue  # repeat the loop if it can't 
if units is None:
    exit() # out of inputs

return units * conversion_rate