python2.7練習小例子(二十六)

    26):題目:給一個很少於5位的正整數,要求:1、求它是幾位數,2、逆序打印出各位數字。python

    程序分析:學會分解出每一位數。app

    程序源代碼:測試

 


#!/usr/bin/python # -*- coding: UTF-8 -*-  x = int(raw_input("請輸入一個數:\n")) a = x / 10000 b = x % 10000 / 1000 c = x % 1000 / 100 d = x % 100 / 10 e = x % 10  if a != 0:  print "5 位數:",e,d,c,b,a elif b != 0:  print "4 位數:",e,d,c,b, elif c != 0:  print "3 位數:",e,d,c elif d != 0:  print "2 位數:",e,d else:  print "1 位數:",e 

    以上實例輸出結果爲:spa

 

請輸入一個數: 23459 5 位數: 9 5 4 3 2

    

請輸入一個數: 3472 4 位數: 2 7 4 3

    

#!/usr/bin/python # -*- coding: UTF-8 -*- print( '請輸入大於10的數字:' ) n=input() x=[] i=0; while(n!=0): x.append(n%10) i+=1 n/=10 print( '該數有 %d 位\n' %i ) print( '逆序爲:\n') print( x[::] )

    輸出實例:.net

 

請輸入大於10的數字: 12345 該數有 5  逆序爲: [5, 4, 3, 2, 1]

    

#!/usr/bin/python # -*- coding: UTF-8 -*- print '輸入大於10的數字:' n=input() x=str(n) for i in range(len(x)-1,-1,-1): print x[i], # , 號設置不換行

    測試輸出結果:code

 

輸入大於10的數字: 12345 5 4 3 2 1

    

#!/usr/bin/python # -*- coding: UTF-8 -*- print( '請輸入大於10的數字:' ) a =input() if (len(str(a)) > 0) and (len(str(a)) <= 5) : print "%s 是 %d 位數" %(a, len(str(a))) newstr = str(a)[::-1] print newstr for i in newstr: print i

    

#!/usr/bin/python # -*- coding: UTF-8 -*- def output(num,l):     if l == 0:         return     print (num[l-1]),     output(num,l-1) num = raw_input('輸入小於5位正整數 :' ) l = len(num) output(num,l) print '\n長度爲: %d' % l

    Python3 下使用列表的 reverse 方法:orm

 

#!/usr/bin/env python3 num = list(input('輸入一個最多5位的數字:')) print(len(num)) num.reverse() for i in range(len(num)): print(num[i], end='')

    

#!/usr/bin/python3 # coding:utf-8 s = str(input()) def fun(m): if len(m) == 1: return m[0] else: return (m[len(m) - 1] + fun(m[:(len(m) - 1)])) if len(s) > 5: print("輸入數字超過限定位數,輸入無效") else: print('數位:%s\n輸入的數字:%s\n逆序數字:%s' % (len(s), s, fun(s)))

    

#!/usr/bin/python # coding:utf-8 arr=[] def out_num(n): length = len(str(n)) print "%d位數"%length for i in range(1, length+1): arr.append((n % (10**i))/10**(i-1)) print arr out_num(245984)

    

#!/usr/bin/env python # -*- coding: UTF-8 -*- import sys s = 12345 s = str(s)[::-1] print '%d 位數' % len(s) for i in range(len(s)): sys.stdout.write(s[i]+' ')

    Python3 測試實例:blog

 

# coding:utf-8 num=int(input("請輸入一個正整數:")) def fn(s): if len(s)==1: return(s[0]) else: a=s[-1] s=s[:-1] return(a+fn(s)) while 1: if num<=0 or len(str(num))>5: num=int(input("輸入錯誤,請從新輸入:")) else: num=str(num) print() print("它是%d位數" % len(num)) print("逆序打印:",fn(num)) break

    

#!/usr/bin/python # -*- coding: UTF-8 -*- def num(strl): l = len(str(strl)) lis = [] for i in range(l): x = strl % 10 strl /= 10 lis.append(x) print "長度爲:{0}, \n逆序爲:{1}".format(l,''.join(str(x) for x in lis) ) num(123)

    Python3 下測試:utf-8

 

#!/usr/bin/python3 x=input('請輸入一個數:\n') a = len(x) print('這是一個{}位數'.format(a)) b = -1 while a != 0: a -= 1 if b == -1: print(x[-1:],end=' ') b=b-1 else : print(x[b:b+1],end=' ') b=b-1

    Python3 下測試:
input

 

#!/usr/bin/python3 def backn(n): ls = str(n); s = len(ls) ls = ls[s-1:0:-1] + ls[0] return ls, s #測試: backn(123456) #輸出:(['654321'], 6)

    

#!/usr/bin/python # -*- coding: UTF-8 -*- num1=int(input("Please input a int <10000:")) for i in range(4,1,-1):     if num1>10**i:         print(i+1)         break list1=[] for j in range(i,-1,-1):     list1.append(int(num1/(10**j)))     num1=num1%(10**j) for k in range(i,-1,-1):     print(list1[k])

    Python3 下測試:

 

number = 12344 print("數字%d," % number, end="") n = 0 result = "" while number > 0: result += str(number % 10) number //= 10 n += 1 print("是%d位數, 逆序: %d" % (n, int(result)))

    主要內容就是數字操做。若是感受不錯的話請多多點贊支持哦。。。

  原文連接:https://blog.csdn.net/luyaran/article/details/80074827

相關文章
相關標籤/搜索