0
votes
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?

2
Do remember to "accept" an answer so the question can be properly retired. This can include blending all the help you got, writing an answer yourself, and accepting that "best of all possible worlds".Prune

2 Answers

3
votes

It's just a small confusion in managing both exit conditions. Check both of them in the while statement, and you'll be fine. I tested this with sequences (-1, -1, -1), (-1, -1, 5), and (-1, 5). It worked fine each time.

counter = 0
miles = float(input('How many miles do you want converted into kilometers? '))
while miles < 0 and counter <= 2:
    print('You cannot enter a negative value!')
    miles = float(input('Enter the correct number of miles: '))
    counter = counter + 1

if counter <= 2:
    milesToKm = (miles*1.6)
    print(miles, 'miles is', round(milesToKm,2), 'kilometers')
else:
    print('Error: cannot exceed three attempts')
    exit()

Another way to handle this is with the else clause of a loop. If you exit the loop normally, its else clause gets executed; if you break out of the loop, the else gets skipped. This lets you handle your logic with a for loop:

for counter in range(3):
    miles = float(input('How many miles do you want converted into kilometers? '))
    if miles >= 0:
        break
    print('You cannot enter a negative value!')

else:
    print('Error: cannot exceed three attempts')
    exit()

milesToKm = (miles*1.6)
print(miles, 'miles is', round(milesToKm,2), 'kilometers')
0
votes

Your problem is that after you go through the while loop at the top, your counter will be greater than 2. That is, 3. Now, your print function is inside an if counter <= 2 statement. 3 is not less than or equal to 2. That is your problem.

counter = 0

while miles < 0:
    # ...
    if counter > 2:
        print('Error: cannot exceed three attempts')
        break

print(miles, 'miles is', round(milesToKm,2), 'kilometers')