Python2

作者: 程序设计法师 | 来源:发表于2019-01-04 18:01 被阅读0次
  • 生成随机数
import random

puter = random.randint(1, 3)
print(str(puter))
  • 多个格式化数据类型
In [22]: player=3

In [23]: print("玩家要出去的 %d 但是是不是 %d" %(player,1))
玩家要出去的 3 但是是不是 1
  • while循环
i = 1
while i <= 5:
    print("执行while")
    i = i + 1
  • python中不能用string+整形来拼接字符串,必须用字符串+字符串
result=0
print("100以内的偶数之和为:" + str(result))
  • continue不执行本次循环(直接跳转到条件语句),break不执行所有循环,跳出
j = 0
while j < 10:
    if j == 3:
        # 在使用continue关键字之前需要确认循环的计数是否修改,否则可能会导致死循环
        j += 1
        continue
    print(j)
    j += 1
0
1
2
4
5
6
7
8
9
  • print函数会在输出内容之后,自动在内容末尾增加换行,加上end=""标识忽略换行符
print("*", end="")
print("*")
**
  • 制表符\t的作用是在不使用表格的情况下在垂直方向上按列对齐文本
1 * 1 = 1  1 * 2 = 2  1 * 3 = 3  1 * 4 = 4  1 * 5 = 5  1 * 6 = 6  1 * 7 = 7  1 * 8 = 8  1 * 9 = 9  
2 * 2 = 4  2 * 3 = 6  2 * 4 = 8  2 * 5 = 10  2 * 6 = 12  2 * 7 = 14  2 * 8 = 16  2 * 9 = 18  
3 * 3 = 9  3 * 4 = 12  3 * 5 = 15  3 * 6 = 18  3 * 7 = 21  3 * 8 = 24  3 * 9 = 27  
4 * 4 = 16  4 * 5 = 20  4 * 6 = 24  4 * 7 = 28  4 * 8 = 32  4 * 9 = 36  
5 * 5 = 25  5 * 6 = 30  5 * 7 = 35  5 * 8 = 40  5 * 9 = 45  
6 * 6 = 36  6 * 7 = 42  6 * 8 = 48  6 * 9 = 54  
7 * 7 = 49  7 * 8 = 56  7 * 9 = 63  
8 * 8 = 64  8 * 9 = 72  
9 * 9 = 81  
1 * 1 = 1   1 * 2 = 2   1 * 3 = 3   1 * 4 = 4   1 * 5 = 5   1 * 6 = 6   1 * 7 = 7   1 * 8 = 8   1 * 9 = 9   
2 * 2 = 4   2 * 3 = 6   2 * 4 = 8   2 * 5 = 10  2 * 6 = 12  2 * 7 = 14  2 * 8 = 16  2 * 9 = 18  
3 * 3 = 9   3 * 4 = 12  3 * 5 = 15  3 * 6 = 18  3 * 7 = 21  3 * 8 = 24  3 * 9 = 27  
4 * 4 = 16  4 * 5 = 20  4 * 6 = 24  4 * 7 = 28  4 * 8 = 32  4 * 9 = 36  
5 * 5 = 25  5 * 6 = 30  5 * 7 = 35  5 * 8 = 40  5 * 9 = 45  
6 * 6 = 36  6 * 7 = 42  6 * 8 = 48  6 * 9 = 54  
7 * 7 = 49  7 * 8 = 56  7 * 9 = 63  
8 * 8 = 64  8 * 9 = 72  
9 * 9 = 81  
i = 1
while i <= 5:
    print("执行while")
    i = i + 1
# 求一百以内的偶数之和
k = 0
result = 0
while k < 100:
    if k % 2 == 0:
        print(k)
        result += k
    k += 1
print("100以内的偶数之和为:" + str(result))
j = 0
while j < 10:
    if j == 3:
        # 在使用continue关键字之前需要确认循环的计数是否修改,否则可能会导致死循环
        j += 1
        continue
    print(j)
    j += 1
print("*", end="")
print("*")
# 打印累加星星符
row = 1
while row <= 5:
    print(row * "*")
    row += 1
# 打印累加星星符
line = 1
while line <= 5:
    line_sub = 1
    while line_sub <= line:
        print("*", end="")  # 忽略换行
        line_sub += 1
    print("")  # 添加换行
    line += 1
# 打印九九乘法表
c = 1
d = 1
while c <= 9:
    while d <= 9:
        # print(str(c) + "*" + str(d), end="   ")
        # 制表符的功能是在不使用表格的情况下在垂直方向上按列对齐文本
        print("%d * %d = %d" % (c, d, c * d), end="\t")
        d = d + 1
    print("")
    c = c + 1
    d = c
执行while
执行while
执行while
执行while
执行while
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100以内的偶数之和为:2450
0
1
2
4
5
6
7
8
9
**
*
**
***
****
*****
*
**
***
****
*****
1 * 1 = 1   1 * 2 = 2   1 * 3 = 3   1 * 4 = 4   1 * 5 = 5   1 * 6 = 6   1 * 7 = 7   1 * 8 = 8   1 * 9 = 9   
2 * 2 = 4   2 * 3 = 6   2 * 4 = 8   2 * 5 = 10  2 * 6 = 12  2 * 7 = 14  2 * 8 = 16  2 * 9 = 18  
3 * 3 = 9   3 * 4 = 12  3 * 5 = 15  3 * 6 = 18  3 * 7 = 21  3 * 8 = 24  3 * 9 = 27  
4 * 4 = 16  4 * 5 = 20  4 * 6 = 24  4 * 7 = 28  4 * 8 = 32  4 * 9 = 36  
5 * 5 = 25  5 * 6 = 30  5 * 7 = 35  5 * 8 = 40  5 * 9 = 45  
6 * 6 = 36  6 * 7 = 42  6 * 8 = 48  6 * 9 = 54  
7 * 7 = 49  7 * 8 = 56  7 * 9 = 63  
8 * 8 = 64  8 * 9 = 72  
9 * 9 = 81  

相关文章

网友评论

      本文标题:Python2

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