1 #题目: Give a positive integer not more than 5 bits, requirements: First, it is a number of digits, second, in reverse order to print out the figures.
Code:
2 3 li = List (input (' Enter a positive integer not more than 5 bits: ')) 4 n = Len (LI) 5 print (' This positive integer is%d digits '%n) 6 i = 0 7 8 def order (Li,n,i): 9 If I <= n-1-i:10 tmp = li[i] li[i] = li[n-1-i] [li[n-1-i] = tmp i + = 1 14 Order (Li,n,i) return (LI) print (Order (li,n,i))
Operation Result:
[[email protected] code_100]# python code_29.py Enter a positive integer with no more than 5 bits: 25689 This positive integer is 5 digits [' 9 ', ' 8 ', ' 6 ', ' 5 ', ' 2 '][[email protected] C ode_100]# [[email protected] code_100]# python code_29.py Enter a positive integer with no more than 5 bits: 487 This positive integer is 3 digits [' 7 ', ' 8 ', ' 4 '][[email protected] code_100]#
Code Explanation:
# lazy, reference Example 2 3 Li = List (input (' Enter a positive integer not more than 5 bits: ')) 4 n = Len (LI) 5 print (' This positive integer is%d digits '%n) 6 i = 0 7 8 def order (l I,n,i): 9 If I <= n-1-i:10 tmp = li[i] one li[i] = li[n-1-i] [li[n-1-i] = tmp 13 i + = 1 (li,n,i) return (LI) print (Order (li,n,i))
This article is from the "Learning Notes" blog, so be sure to keep this source http://netsyscode.blog.51cto.com/6965131/1749071
"Python" programming language Introduction Classic 100 cases--29