'if statement after input wont compare to number
Im trying to write a program that asks every user for their age, if they are less than 16 years old it should remove them from the list.
Problem is i keep getting the following error - "TypeError: '<' not supported between instances of 'str' and 'int'"
Even though i made sure the input is an "int", my "if" statement will not let me compare it to a numerical value (if i < 16).
Any help would be appreciated, all of the solutions i found online say i must use the "int" before the input, but i already did.
list_names = ['Daniel', 'Samuel', 'Ruth', 'David']
for i in list_names:
int(input('What is your age? '))
if i < 16:
list_names.remove(i)
print(list_names)
Solution 1:[1]
list_names = ['Daniel', 'Samuel', 'Ruth', 'David']
for i in list_names:
age = int(input('What is your age? '))
if age < 16:
list_names.remove(i)
print(list_names)
you are comparing string with an integer. i is a string and 16 is an integer. So, your if condition does not work.
Also, you should save the input in a variable if you want to compare it. Just writing int(input("...")) will ask for input, but you won't be able to use it anywhere. That is why I saved it in age
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aryan Arora |
