【发布时间】:2013-09-09 06:35:33
【问题描述】:
I'm taking an Intro to Python course online 并希望更有效地解决这个问题。
单词 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th 称为序数形容词。编写一个程序,从输入中读取一个介于 1 和 9 之间的整数 x。程序应该输出对应于 x 的序数形容词。 提示:您不需要有 9 个单独的案例; 4个就够了。
x = int(input())
if x == 1:
print("1st")
elif x == 2:
print("2nd")
elif x == 3:
print("3rd")
elif x == 4:
print("4th")
elif x == 5:
print("5th")
elif x == 6:
print("6th")
elif x == 7:
print("7th")
elif x == 8:
print("8th")
elif x == 9:
print("9th")
我无法弄清楚如何用四行更有效地编写此代码。是否涉及列表?他们还没有教过列表或数组。
【问题讨论】:
标签: python if-statement