【发布时间】:2018-04-25 01:07:25
【问题描述】:
这里真的需要一些帮助。超级早学 Python。
目标是取一个数字,看看数字是否按升序排列。 到目前为止我所拥有的是:
a = int(input("Enter a 4-digit number: "))
b = [int(i) for i in str(a)]
if b[0] > b[1]:
print "Not ascending"
elif b[1] > b[2]:
print "Not ascending"
elif b[2] > b[3]:
print "Not ascending"
else:
print "Ascending!"
我的问题是,我怎样才能使输入的位数没有限制?因此,如果有人输入一个 7 位数字,它会执行相同的过程直到最后一位。
【问题讨论】:
-
使用
for循环 -
"".join(sorted(a)) == a -
保持输入为字符串和
list(a) == sorted(list(a))。
标签: python python-3.x