pythonuser-input

ValueError: invalid literal for int() with base 10: 'done'. what can i do?


I wrote this code and tested it, but in the end I encountered a problem and an error, because the number should be entered inside the input, but inside the if, when the user enters the word done, the loop should be closed, and the problem is in the type.

while True:
    result = int(input('enter a number: \n>'))
    if result == 'done': 
        break 

According to the explanations I gave, my problem is in the type and the loop should stop when the word is entered


Solution

  • Your casting your input to type int(). Instead it needs to be a string because when the user types "done" into the input, it cannot be cast into an integer

    while True:
        result = input('enter a number: \n>')
        if result == 'done': 
            break
    

    if you want to then use it as a number if they did not type done add an else clause to the if statement:

    else:
       result = int(result)