我需要在 python 3 中将无限表示为整数
I need to represent infinite as integer in python 3
我在 Python 3,Windows 10,PyCharm 工作。
我正在构建一个小程序,让您输入年龄和 returns "Happy {age}{termination, eg: st, nd}!"
问题是我想以某种方式避免您将年龄写为 1041 而它会显示 "Happy 1041th!" 的情况。因此,例如,我使用 list(range(21, 1001, 10))
作为终止符 "st"。但是,我希望能够使用 infinite
而不是 1001。如果我使用 math.inf
,那是一个浮点数,我的代码不接受它。另外,我无法将其转换为 int。
我正在考虑使用一个 n
的数字,它应该大于 100,并且有 list(range(21, n, 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 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)
模运算是解决此问题的更好(且完全通用)的解决方案:
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)
没有理由检查大量 list
中的成员资格,这会占用大量内存。你可以只检查你的年龄 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)
不需要改成list,如果改成list可能会报错。您只需输入 age in range(s, e, i)
。如果你想要更高的无限使用这样 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)
但是,如果有比它更简单的方法,为什么要使用 range
?喜欢让它成为 string
然后检查最后一个数字。
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)
是的,我知道结果不同。但是,我向您展示的下一个代码也可以处理超过 100 个。比如101,你的代码上会是第101位;我认为是不正确的。变量 term
我输入了 ternary operator
或 conditional expression
。
在 Python [if_true] if [condition] else [if_false]
在 JS condition? if_true:if_false
Looping from 1 to infinity in Python
我在 Python 3,Windows 10,PyCharm 工作。
我正在构建一个小程序,让您输入年龄和 returns "Happy {age}{termination, eg: st, nd}!"
问题是我想以某种方式避免您将年龄写为 1041 而它会显示 "Happy 1041th!" 的情况。因此,例如,我使用 list(range(21, 1001, 10))
作为终止符 "st"。但是,我希望能够使用 infinite
而不是 1001。如果我使用 math.inf
,那是一个浮点数,我的代码不接受它。另外,我无法将其转换为 int。
我正在考虑使用一个 n
的数字,它应该大于 100,并且有 list(range(21, n, 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 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)
模运算是解决此问题的更好(且完全通用)的解决方案:
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)
没有理由检查大量 list
中的成员资格,这会占用大量内存。你可以只检查你的年龄 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)
不需要改成list,如果改成list可能会报错。您只需输入 age in range(s, e, i)
。如果你想要更高的无限使用这样 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)
但是,如果有比它更简单的方法,为什么要使用 range
?喜欢让它成为 string
然后检查最后一个数字。
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)
是的,我知道结果不同。但是,我向您展示的下一个代码也可以处理超过 100 个。比如101,你的代码上会是第101位;我认为是不正确的。变量 term
我输入了 ternary operator
或 conditional expression
。
在 Python [if_true] if [condition] else [if_false]
在 JS condition? if_true:if_false
Looping from 1 to infinity in Python