Python Exercise Example 83
**Problem:** Find the number of odd numbers that can be formed using 0-7.
Program analysis:
There are 4 one-digit numbers that can be formed.
There are 7*4 two-digit numbers that can be formed.
There are 7 three-digit numbers that can be formed84 numbers in total.
There are 7 four-digit numbers that can be formed88*4 numbers in total.
......
Program source code:
Example (Python 2.0+)
#!/usr/bin/python
-- coding: UTF-8 --
if
name
==
'
main
'
:
sum
=
4
s
=
4
for
j
in
range
(
2
,
9
)
:
sum
if
j
<=
2
:
s
*=
7
else
:
s
*=
8
sum
+=
s
'
sum = %d
'
%
sum
The output of the above example is:
4
32
256
2048
16384
131072
1048576
sum = 8388608
Python 100 Examples