**
*
**
**
*
**
***
row = 1
while row <= 5:
# 每一行要打印的星星就是和当前行数是一致的
col = 1
while col <= row:
# 不换行输出
print('*',end='')
col += 1
print('第%d行' % row)
# 手动换行
print()
row += 1
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
row = 1
while row <= 9:
col = 1
while col <= row:
print('%d * %d = %d\t' %(row,col,col*row),end='')
col += 1
print()
row += 1
print('1 2 3')
print('10 20 30')
# \t:在控制台输出一个制表符,协助我们在输出文本时在垂直方向保持对齐
print('1\t2\t3')
print('10\t20\t30')
猜数字游戏
if , while(for), break
1. 系统随机生成一个1~100的数字;
** 如何随机生成整型数, 导入模块random, 执行random.randint(1,100);
2. 用户总共有5次猜数字的机会;
3. 如果用户猜测的数字大于系统给出的数字,打印“too big”;
4. 如果用户猜测的数字小于系统给出的数字,打印"too small";
5. 如果用户猜测的数字等于系统给出的数字,打印"恭喜中奖100万",并且退出循环;
import random
x = random.randint(1,100)
print(x)
trycount = 0
while trycount < 5:
tk = int(input('请输入你猜测的数字:'))
if tk == x:
print('恭喜中奖100万')
break
elif tk < x:
print('too small')
trycount += 1
else:
print('too big')
trycount += 1
else:
print('机会已经用完了~')