我们有一项令我难以理解的作业。我们必须编写一个名为int2ordinal的函数来解决以下任务:
以11,12或13结尾的任何数字都使用后缀
以1结尾的所有剩余数字都使用st后缀
以2结尾的所有剩余数字都使用nd后缀
以3结尾的所有剩余数字都使用rd后缀
所有剩余的数字都使用后缀
所以基本上编写一个名为int2ordinal的函数,它接受一个整数作为唯一参数,并返回带有适当后缀的数字作为结果(存储在字符串中)。例如,如果函数被传递了 整数1然后它应该返回字符串“1st”。如果它传递整数12,那么它应该返回字符串“12th”。如果它通过了2003,那么它应该返回字符串“2003rd”。
我们使用余数运算符通过计算整数除以10时的余数来提取整数的最后一位,我们也是这样做以提取整数的最后两位除以100。例如29%10是9而1911%100是11.然后我们可以通过将整数参数转换为字符串并使用+运算符连接适当的后缀来构造函数需要返回的字符串。
这是我们到目前为止的代码,我们认为上半部分是完全错误的:
def int2ordinal(day, month, year):
if day % 10 == 1: return (str(day + "st"))
if day % 10 == 2: return (str(day + "nd"))
if day % 10 == 3: return (str(day + "rd"))
if month % 10 == 1: return (str(month + "st"))
if month % 10 == 2: return (str(month + "nd"))
if month % 10 == 3: return (str(month + "rd"))
if year % 100 == 1: return (str(year + "st"))
if year % 100 == 2: return (str(year + "nd"))
if year % 100 == 3: return (str(year + "rd"))
for i in range (11, 13): return (str(day + "th"))
for i in range (11, 13): return (str(month + "th"))
for i in range (11, 13): return (str(year + "th"))
else: return (str(day + "th", month + "th", year + "th"))
def main():
day = int(input("Enter a day between 1 and 31: "))
month = int(input("Enter a month between 1 and 12: "))
year = int(input("Enter a year between 1 and 2100: "))
print("On the", int2ordinal(day), "day of the", int2ordinal(month), \
"month of the", int2ordinal(year), "year, something amazing happened!")
main()
任何帮助都会很棒!
答案 0 :(得分:1)
为什么不让它更简单?
您可以在下面找到此代码,并使用pip install mknxgn_essentials
然后使用asordinal = essentials.GetOrdinal(my_num)
def GetOrdinal(number):
if type(number) != type(1):
try:
number = int(number)
except:
raise ValueError("This number is not an Int!")
lastdigit = int(str(number)[len(str(number))-1])
last2 = int(str(number)[len(str(number))-2:])
if last2 > 10 and last2 < 13:
return str(number) + "th"
if lastdigit == 1:
return str(number) + "st"
if lastdigit == 2:
return str(number) + "nd"
if lastdigit == 3:
return str(number) + "rd"
return str(number) + "th"
答案 1 :(得分:-1)
为什么不保持简单?虽然未经测试,但这应该有用。
def int2ordinal(num):
def formatted_num(suffix): # Nested function.
return str(num) + suffix
end_digits = int(num[-2:]) # Gets last 2 chars of the string. Instead of repeating this operation on every if clause, better do it here.
# You only really need to check if it either finishes with 1,2,3 - remaining values will have a th suffix.
if end_digits in [1]:
return formatted_num('st')
elif end_digits in [2]:
return formatted_num('nd')
elif end_digits in [3]:
return formatted_num('rd')
else:
return formatted_num('th')
def main():
day = input("Enter a day between 1 and 31: ")
month = input("Enter a month between 1 and 12: ")
year = input("Enter a year between 1 and 2100: ")
print("On the", int2ordinal(day), "day of the", int2ordinal(month), \
"month of the", int2ordinal(year), "year, something amazing happened!")
main()
根据您在评论中的回答,然后执行以下操作:
def int2ordinal(num):
num = str(num)
if len(num) > 2: # Will be a year, not necessarily consisting of four digits. Could be 333
end_digits = int(num) % 100
else: # Will be month / day
end_digits = int(num) % 10
if end_digits == 1: return (num + "st")
if end_digits == 2: return (num + "nd")
if end_digits == 3: return (num + "rd")
else: return (num + "th")
def main():
day = input("Enter a day between 1 and 31: ")
month = input("Enter a month between 1 and 12: ")
year = input("Enter a year between 1 and 2100: ")
print("On the", int2ordinal(day), "day of the", int2ordinal(month), \
"month of the", int2ordinal(year), "year, something amazing happened!")
main()
一般情况下,尽量避免重复代码。如果你查看原始代码,你就会在每个子句上运行num%10/100,并且每次返回都会转换为str(这里没有性能损失,但重复逻辑)。最好避免尽可能多地重复这样的调用,即使这段代码不是资源密集型的,因为你说你是业余爱好者:越早学习,越好。
另外,原始代码总是失败的原因是它有三个参数,你总是花一天时间去它。命名参数的工作方式如下:
def foo(a,b,c):
pass
foo(a=0)
foo(b=1)
foo(c=2)
操作:
foo(3)
foo()
获得参数a
,b和c的结果将为None
。