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âŹâ)
4 Answers
+ 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... đ
+ 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")
+ 2
Made bit changesâșïž
https://code.sololearn.com/cDKP1XdXCwti/?ref=app
+ 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âŹ")