Reputation: 45
I'm working in Python 3, Windows 10, PyCharm.
I'm building a small program that lets you input your age and returns "Happy {age}{termination, eg: st, nd}!"
The thing is that I want to somehow avoid the situation where you will write your age as 1041 and it'll say "Happy 1041th!". So I used list(range(21, 1001, 10))
for the termination "st", for instance. However, I'd like to be able to use infinite
instead of 1001. If I use math.inf
, that's a float and it's not accepted in my code. Also, I can't convert it to int.
I'm thinking of using a n
number that should be higher than say, 100, and have list(range(21, n, 10))
, but I'm too much of a beginner to know how to do that.
Thanks for the help. Here's my code:
age = int(input('Type age: '))
if int(age) == 1:
term = 'st'
elif int(age) == 2:
term = 'nd'
elif int(age) == 3:
term = 'rd'
elif int(age) in list(range(21, 1001, 10)):
term = 'st'
elif int(age) in list(range(22, 1002, 10)):
term = 'nd'
elif int(age) in list(range(23, 1003, 10)):
term = 'rd'
else:
term = 'th'
if int(age) >= 130:
print("C'mon! you can't be THAT old, you geezer!\nStill, here you go:")
message = f"Happy {age}{term} birthday!"
print(message)
Upvotes: 1
Views: 578
Reputation: 101
You don't need change to be list, if you change it to be list it may will be error. You just input age in range(s, e, i)
. If you want higher like as infinity use like this age in range(21, sys.maxsize**100, 10)
import sys
inf = sys.maxsize**10
age = int(input('Type age: '))
if int(age) == 1: term = 'st'
elif int(age) == 2: term = 'nd'
elif int(age) == 3: term = "rd"
elif int(age) in range(21, inf, 10): term = 'st'
elif int(age) in range(22, inf, 10): term = "nd"
elif int(age) in range(23, inf, 10): term = 'rd'
else: term = 'th'
if int(age) >= 130:
print("C'mon! you can't be THAT old, you geezer!\nStill, here you go:")
message = f"Happy {age}{term} birthday!"
print(message)
But, why you want to use range
if there is easier than it? Like make it to be string
and then check the last number.
age = int(input("Type age: "))
term = ["st", "nd", "rd", "th"][3 if age%10 > 3 or age%100 in range(10, 20) else age%10-1]
if age > 130: message = "blah-blah-blah"
print(message)
Yes, I know the result is defferent. But, the next code I show you it's also can handle higher than hundred. Such as 101, on your code will be 101th; I think isn't correct. Variable term
I have input ternary operator
or conditional expression
.
In Python [if_true] if [condition] else [if_false]
In JS condition? if_true:if_false
Looping from 1 to infinity in Python
Upvotes: 1
Reputation: 4365
There is no reason to check membership in a massive list
which will eat up a ton of memory. You can just check what you age endswith
.
age = input('Type age: ')
if age.endswith('11') or age.endswith('12') or age.endswith('13'):
term = 'th'
elif age.endswith('1'):
term = 'st'
elif age.endswith('2'):
term = 'nd'
elif age.endswith('3'):
term = 'rd'
else:
term = 'th'
if int(age) >= 130:
print("C'mon! you can't be THAT old, you geezer!\nStill, here you go:")
message = f"Happy {age}{term} birthday!"
print(message)
Upvotes: 3
Reputation: 147156
Modulo arithmetic is a better (and completely general) solution to this problem:
age = int(input('Type age: '))
if 11 <= (age % 100) <= 13:
term = 'th'
elif age % 10 == 1:
term = 'st'
elif age % 10 == 2:
term = 'nd'
elif age % 10 == 3:
term = 'rd'
else:
term = 'th'
if age >= 130:
print("C'mon! you can't be THAT old, you geezer!\nStill, here you go:")
message = f"Happy {age}{term} birthday!"
print(message)
Upvotes: 3