-1
power=input("How much power would you like to have?(power goes from 1 to a 100)")
while power > 100 or power < 0:
    if power < 100 or power > 0:
        break
    else:
        power=input("How much power would you like to have?")

when i try to run this part of a code it keeps showing an error message that looks like: while puissance > 100 or puissance < 0: TypeError: '>' not supported between instances of 'str' and 'int'

Samwise
  • 68,105
  • 3
  • 30
  • 44
PolS
  • 1
  • 3

1 Answers1

1

The input function returns a string (str). To convert it to an int you need to use the int function:

power = int(input("How much power would you like to have?(power goes from 1 to a 100)"))

Note that int() will raise a ValueError if the string the user inputs isn't one that can be interpreted as an integer.

If you want to repeatedly prompt the user until they provide a valid value, use a loop with a try/except:

while True:
    try:
        power = int(input(
            "How much power would you like to have? (power goes from 1 to 100)"
        ))                        # raises ValueError if not an int
        assert 1 <= power <= 100  # raises AssertionError if not in range
    except (AssertionError, ValueError):
        continue  # prompt again
    else:
        break     # continue on with this power value
Samwise
  • 68,105
  • 3
  • 30
  • 44