减少小型python脚本中条件的数量
我一直在开发一个小程序。 它的工作原理非常完美,但我想让代码稍微小一点减少小型python脚本中条件的数量,python,Python,我一直在开发一个小程序。 它的工作原理非常完美,但我想让代码稍微小一点 import time, math name= input("Enter Your Name: ") age= int(input("Enter Your Age: ")) end= "th" if age == 3 or age == 13 or age == 23 or age == 33 or age == 43 or age == 53 or age == 63 or age == 73 or age == 83
import time, math
name= input("Enter Your Name: ")
age= int(input("Enter Your Age: "))
end= "th"
if age == 3 or age == 13 or age == 23 or age == 33 or age == 43 or age == 53 or age == 63 or age == 73 or age == 83 or age == 93:
end= "rd"
if age == 2 or age == 22 or age == 32 or age == 42 or age == 52 or age == 62 or age == 72 or age == 82 or age == 92:
end= "nd"
print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(5)
我想用一种更简单的方法将“end”更改为其他值,而不必全部写入。我可以让它从3开始,然后对所有10个以上的值执行此操作。使用:
或者使用口述:
ends = {2: "nd", 3: "rd"}
end = ends[age % 10]
您还可以使用默认值:
ends = {1: "st", 2: "nd", 3: "rd"}
end = ends.get(age % 10, "th)
您可以尝试以下方法:
if int(age[-1]) == 3:
end= "rd"
if int(age[-1]) == 2:
end= "nd"
提取第十位的数字。那就直截了当了。虽然这个问题属于SO的
codereview
对应问题
import time, math
name= input("Enter Your Name: ")
age= int(input("Enter Your Age: "))
tenth_place = age % 10
if tenth_place == 3:
end = "rd"
elif tenth_place == 2:
end = "nd"
else:
end = "th"
print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(5)
可能不一定更短,但它可以工作(并保持第12和第13位正确)
谢谢大家,伙计们 我还发现了另一个缺陷并修复了它,这是我当前的代码。 谢谢
导入时间、数学
name=输入(“输入您的姓名:”)
年龄=整数(输入(“输入您的年龄:”)
end=“th”
如果年龄%10==3:
end=“rd”
elif年龄%10==2:
end=“nd”
elif年龄%10==1:
end=“st”
如果年龄<20岁且年龄>10岁:
end=“th”
打印(“你的名字是“+Name+”,你是“+str(年龄)+”岁。”)
打印(“嗨”+姓名+”,快乐“+str(年龄)+结束+“生日!”)
时间。睡眠(2)
谢谢,
Bilbo请写一个更好地描述你的问题的标题。你应该环顾一下网站,看看如何写一个描述性的标题——你现在的标题不是特别清楚。另外,查看Python中的模运算符(提示:您可以用
age%10==3
)替换您的第一个检查。这里已经回答了这个问题:而且,您似乎遗漏了整个案例……好的,下次C.dlp感谢大家的回答时,我会告诉大家,它成功了perfectly@Bilbo你应该考虑接受这个答案,在这种情况下,谢谢大家的想法,
if int(age[-1]) == 3:
end= "rd"
if int(age[-1]) == 2:
end= "nd"
import time, math
name= input("Enter Your Name: ")
age= int(input("Enter Your Age: "))
tenth_place = age % 10
if tenth_place == 3:
end = "rd"
elif tenth_place == 2:
end = "nd"
else:
end = "th"
print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(5)
import time, math
name = input("Enter Your Name:")
age = int(input("Enter Your Age:"))
end = "th"
# initializing age lists
list1 = []
list2 = []
# filling list1 with ages 3-93
for i in range(0,10):
list1.append(10*i+3)
# filling list2 with ages 2-92
for i in range(0,10):
list2.append(10*i+2)
# if block to include correct suffix
for ages in list1:
if ages == 13:
end = end;
elif ages == age:
end = "rd"
for ages in list2:
if ages == 12:
end = end
elif ages == age:
end = "nd"
print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(5)
import time, math
name= input("Enter Your Name: ")
age= int(input("Enter Your Age: "))
end= "th"
if age % 10 == 3:
end = "rd"
elif age % 10 == 2:
end = "nd"
elif age % 10 == 1:
end = "st"
if age < 20 and age > 10:
end = "th"
print ("Your Name Is "+ name + ", You Are " + str(age) + " Years Old.")
print ("Hi " + name + ", Happy " + str(age) + end + " birthday!")
time.sleep(2)