編寫一段代碼,輸入一個正整數,求解該整數的位數並逆序打印各位數
def checkValid(n):
try:
if int(n) < 0:
return False
if n == "-0":
return False
except ValueError:
return False
def askNumber(n):
global num
if n == 0:
num = input('Enter any positive number:')
else:
num = input('Please enter any positive number again:')
askNumber(0)
while True:
if checkValid(num) == False:
askNumber(1)
else:
print("")
print('There are',len(num),'digits in total')
print('And the descending order is following:')
reversedArray = reversed(num)
print(*reversedArray, sep=", ")
break
while True:
a = input('Enter a positive num: ')
try:
b = int(a)
if b > 0:
break
else:
pass
except:
pass
print(f'The input postive num has {len(a)} digits')
b = a[::-1]
for i in b:
print(int(i),end = ' ')
輸出結果:
Enter a positive num: asdf
Enter a positive num: -83224
Enter a positive num: 0
Enter a positive num: 23489
The input postive num has 5 digits
9 8 4 3 2