I have an assignment for my programming fundamentals class. In this, I have to accept invalid inputs a certain number of times (in this case, five) before the program terminates. The book doesn't seem to explain limiting number of loop runs in this context, so I've included my code for guidance. As written, it continues to loop and doesn't stop.
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 ()