python-while练习


**




*

**







**
*




**
***

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('机会已经用完了~')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值