擅长:python、mysql、java
<p>稍微修改代码:</p>
<pre><code>names = {1: "first", 2: "second", 3: "third" # and so on...
}
items = []
for i in range(5):
item = input("Input your {} thing: ".format(names[i+1])
items.append(item)
print(items)
</code></pre>
<p>或更一般的版本:</p>
<p>def getordinal(n):
如果str(n)[-2:]in(“11”,“12”,“13”):
return“{}th”.format(n)
elif str(n)[-1]=“1”:
return“{}st”.format(n)
elif str(n)[-1]=“2”:
return“{}nd”.format(n)
elif str(n)[-1]=“3”:
return“{}rd”.format(n)
其他:
return“{}th”.format(n)</p>
<p>或者更简洁的定义:</p>
<pre><code>def getord(n):
s=str(n)
return s+("th" if s[-2:] in ("11","12","13") else ((["st","nd","rd"]+
["th" for i in range(7)])
[int(s[-1])-1]))
</code></pre>