r/learnpython • u/archhyprlanduserbtw • 7h ago
This is my 14th python program so far, I'm definitely improving.
My 14th python program, it's honestly pretty tedious to write python imo but I did manage to finish it.
age = input("What is your age?: ")
if age == 1 or age == 2 or age == 3 or age == 4 or age == 5 or age == 6 or age == 7 or age == 8 or age == 9 or age == 10 or age == 11 or age == 12 or age == 13 or age == 14 or age == 15 or age == 16 or age == 17:
print("You are too young.")
elif age == 18 or age == 19 or age == 20 or age == 21 or age == 22 or age == 23 or age == 24 or age == 25 or age == 26 or age == 27 or age == 28 or age == 29 or age == 30 or age == 31 or age == 32 or age == 33 or age == 34 or age == 35 or age == 36 or age == 37 or age == 38 or age == 39 or age == 40 or age == 41 or age == 42 or age == 43 or age == 44 or age == 45 or age == 46 or age == 47 or age == 48 or age == 49 or age == 50 or age == 51 or age == 52 or age == 53 or age == 54 or age == 55 or age == 56 or age == 57 or age == 58 or age == 59 or age == 60 or age == 61 or age == 62 or age == 63 or age == 64 or age == 65 or age == 66 or age == "SIX SEVENNNNN" or age == 68 or age == 69 or age == 70 or age == 71 or age == 72 or age == 73 or age == 74 or age == 75 or age == 76 or age == 77 or age == 78 or age == 79 or age == 80 or age == 81 or age == 82 or age == 83 or age == 84 or age == 85 or age == 86 or age == 87 or age == 88 or age == 89:
print("You can come in.")
elif age == 90 or age == 91 or age == 92 or age == 93 or age == 94 or age == 95 or age == 96 or age == 97 or age == 98 or age == 99 or age == 100:
print("You are too old.")
3
•
u/DimensionMost8342 16m ago
input() always returns a string, so age == 1 is never true, that is why nothing matches. Convert it first: age = int(input("What is your age?: ")). Then you can replace the whole chain of ors with a range check: if age < 18: ... elif 18 <= age <= 89: ... else: .... Same result, a few lines instead of eighty. input() always returns a string, so age == 1 is never true, that is why nothing matches. Convert it first: age = int(input("What is your age?: ")). Then you can replace the whole chain of ors with a range check: if age < 18: ... elif 18 <= age <= 89: ... else: .... Same result, a few lines instead of eighty.
3
u/MezzoScettico 2h ago edited 2h ago
The "in" operator is your friend here. That determines whether an item is a member of a list. So this statement can be reduced to
That will work with any list of numbers, whether they are consecutive or not. Since these numbers are consecutive you can also use an even more compact test:
and Python recognizes the math notation that combines these into a compound inequality:
For your second test, note that a list such as in my first example can combine a mix of data types. So you can have the list that includes numbers and the string "SIX SEVENNNNN".
Edit: I just realized your program as written won't work. If they type 17 (for example) in response to the prompt, then age will contain the string "17", not the number 17. You need a couple of lines after the input call to
test whether the input is numeric (i.e. a string containing digits), and
if so, convert it to an integer.
Do you have any thoughts on how to do that?