美文网首页
2018-11-22

2018-11-22

作者: Karl_2c80 | 来源:发表于2018-11-22 23:46 被阅读0次

5) for 循环

  • for : 重复执行语句
# 语法格式如下
for i in sequence:
    statements
else:
    statements
# print语句中使用逗号来禁止换行输出
In [1]: for i in range(5):
   ...:     print i
   ...:     
0
1
2
3
4

In [2]: for i in range(5):
   ...:     print i,
   ...:     
0 1 2 3 4
# xrange()生成的是一个object,定义时不占用内存空间,用法同range()
# 建议多使用xrange来替代range

In [8]: for i in xrange(10):print i,
0 1 2 3 4 5 6 7 8 9

In [9]: n = xrange(10)

In [10]: n
Out[10]: xrange(10)

In [11]: type(n)
Out[11]: xrange
### 乘法口诀表
## 代码如下
#!/usr/bin/python
# -*- coding:utf-8 -*-

for i in xrange(1, 10):
    for j in xrange(1, i+1):
        print "%sx%s=%s" % (j, i, j*i), # 逗号表示输出到一行
    print       # print默认输出换行符

## 结果如下
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

6) while 循环

  • while : 条件为true时执行循环体
# 语法格式如下
while condition_true:
    statements
else:
    statements
# 猜数字游戏
# 用户输入两个整数,系统随机生成一个在这两个数之间的整数,玩家在6次内猜对则获胜

#!/usr/bin/python
# -*- coding:utf-8 -*-

import random

print "A number between the two numbers you input will be generated randomly!"
a = input("Please input a number: ")
b = input("Please input the other number: ")
if a >= b:
    a, b = b, a
c = (a + b) / 2
n = 1
r = random.randint(a, b)

while c != r:
    if r > c:
        a = c
    else:
        b = c
    c = (a + b) / 2
    n += 1
else:
    print "For %s times, I guess: %s" % (n, c)
    print "The random number is", r
    if n <= 6:
        print "Congratulation! You win!"
    else:
        print "Sorry! You lose!"
[root@KARL ~]# python y2.py
A number between the two numbers you input will be generated randomly!
Please input a number: 1
Please input the other number: 20
For 5 times, I guess: 19
The random number is 19
Congratulation! You win!
[root@KARL ~]# python y2.py
A number between the two numbers you input will be generated randomly!
Please input a number: 20
Please input the other number: 1
For 3 times, I guess: 3
The random number is 3
Congratulation! You win!

7) continue & break

  • continue: 跳出本次循环,只跳过本次循环continue后的语句
  • break: 跳出整个循环体,循环体中未执行的循环将不会执行

练习

练习1

  1. 有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
#!/usr/bin/python
# -*- coding:utf-8 -*-

m = []
for x in range(1, 5):
    for y in range(1, 5):
        for z in range(1, 5):
            if x != y and x != z and y != z:
                n = 100 * x + 10 * y + z
                m.append(n)
print "%s numbers were generated as below:" % len(m)
print m
24 numbers were generated as below:
[123, 124, 132, 134, 142, 143, 213, 214, 231, 234, 241, 243, 312, 314, 321, 324, 341, 342, 412, 413, 421, 423, 431, 432]
  1. 打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
#!/usr/bin/python
# -*- coding:utf-8 -*-

m = []
for x in range(1, 10):
    for y in range(10):
        for z in range(10):
            n = 100 * x + 10 * y + z
            if n == x**3 + y**3 + z**3:
                m.append(n)
print m
[153, 370, 371, 407]
  1. 两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
In [28]: t1 = ('a', 'b', 'c')

In [29]: t2 = ('x', 'y', 'z')

In [30]: m = []

In [31]: for k in t1:
    ...:     for v in t2:
    ...:         i = (k, v)
    ...:         m.append(i)
    ...:         

In [32]: m
Out[32]: 
[('a', 'x'),
 ('a', 'y'),
 ('a', 'z'),
 ('b', 'x'),
 ('b', 'y'),
 ('b', 'z'),
 ('c', 'x'),
 ('c', 'y'),
 ('c', 'z')]

In [33]: m.remove(('c', 'x'))

In [34]: m.remove(('c', 'z'))

In [35]: m.remove(('a', 'x'))

In [36]: m
Out[36]: [('a', 'y'), ('a', 'z'), ('b', 'x'), ('b', 'y'), ('b', 'z'), ('c', 'y')]

In [37]: m.remove(('a', 'y'))

In [38]: m.remove(('b', 'y'))

In [39]: m
Out[39]: [('a', 'z'), ('b', 'x'), ('b', 'z'), ('c', 'y')]

In [40]: m.remove(('b', 'z'))

In [41]: m
Out[41]: [('a', 'z'), ('b', 'x'), ('c', 'y')]

练习2

  1. 将一个正整数分解质因数。例如:输入90,打印出90=233*5。
#!/usr/bin/python
# -*- coding:utf-8 -*-

n = input("Please input a number: ")
d = 2   # divisor 除数
q = n   # quotient 商
m = []  # 存放质因数

while q != 1:
    while q % d != 0:
        d += 1
    else:
        m.append(d)
        q /= d
        d += 1
else:
    print m
Please input a number: 120
[2, 3, 4, 5]

Please input a number: 168
[2, 3, 4, 7]
  1. 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
#!/usr/bin/python
# -*- coding:utf-8 -*-

n = 1
d = 10
while d != 1:
    n = (n + 1) * 2
    d -= 1
else:
    print n
1534

相关文章

网友评论

      本文标题:2018-11-22

      本文链接:https://www.haomeiwen.com/subject/ftirqqtx.html