0

TypeError : argument of type ‘int’ is not iterable

Good evening in my code there is an error that I cannot understand I wanted my program to run if age is between 19 and 89 to display you are of age if not display you are a minor. Please help me I'm on it for hours Here is thé code for major in range (19, 89): print (‘you are major’) age = int(input(‘your age?’)) if age in major : print(‘you pay 12€’) else: print(‘you pay 7€’)

10th Jun 2020, 6:51 PM
Marcio Tidjane Agostinho Traore
Marcio Tidjane Agostinho Traore - avatar
4 Respuestas
+ 5
I think it may help you to know why your code didn't work the way you expected, so I'll try and explain. With a for loop, the code will likely loop over it many times. Where you have "for major in range(19, 89):", this is telling the parser that you want to set the variable "major" as the value 19 for the first iteration. Once the code inside the loop is done, it will return to the start of the loop and set major as the value 20 this time. Then the next time around, major will be 21. As you can see, your 'if' line 'if age in major:' doesn't really make sense as, the first time around, you are asking if the age variable (which could be 25 for example) is in 19. This is why you get the error. The others have posted codes that will do what you want, but I felt it would help you to understand what was going on. Hope it helps... 👍
10th Jun 2020, 7:29 PM
Russ
Russ - avatar
+ 3
For loop in python is just for iterables such as lists and tuples and ints aren't iterable in other hand better way for that is: Age = int(input()) If age>19 and age<89: Print("you pay 12") Else: Print("you pay 7")
10th Jun 2020, 7:53 PM
Sadness
Sadness - avatar
10th Jun 2020, 7:10 PM
ANJALI SAHU
+ 2
age = int(input("Your age: ")) if age in range(19,89): print("You are major and you pay 12€") else: print("You pay 7€")
10th Jun 2020, 7:10 PM
Matej Elezović
Matej Elezović - avatar