counter = 0
miles = float(input('How many miles do you want converted into kilometers? '))
while miles < 0:
print('You cannot enter a negative value!')
miles = float(input('Enter the correct number of miles: '))
counter = counter + 1
if counter > 2:
break
if counter <= 2:
milesToKm = (miles*1.6)
print(miles, 'miles is', round(milesToKm,2), 'kilometers')
else:
print('Error: cannot exceed three attempts')
exit()
Hi everybody. This is my fifth week of learning to code. I'm supposed to create a program that will give the user three attempts to enter a valid value. If the value on the third attempt is invalid (a negative number), then it will prompt an error message and terminate.
The problem is that I can enter two invalid values followed by a valid value, and it still gives the error message and terminates. The program should calculate the third valid value and do the mathematics then print the conversion.
For example:
How many miles do you want converted into kilometers? -1
You cannot enter a negative value!
Enter the correct number of miles: -1
You cannot enter a negative value!
Enter the correct number of miles: 5
Error: cannot exceed three attempts
Can you guys help?