1.while循环
- demo01
n=1
while n <= 30:
if(n%3==0 and n%5 ==0):
print('*'*5,n,'*'*5)
n+=1
***** 15 *****
***** 30 *****
- demo02
sum=0
n=1
while n<=100:
sum+=n
n+=1
print('sum=',sum)
sum= 5050
- demo03 打印三角形
ceng=1
while ceng<=4:
print('*'*ceng)
ceng+=1
ceng=1
while ceng<=4:
count=1
while count<=ceng:
print('*', end='')
count+=1
ceng+=1
print()
- demo04 输出99乘法表
ceng=1
while ceng<=9:
count=1
while count<=ceng:
print('{}*{}={} '.format(count, ceng, count*ceng), end='')
count+=1
ceng+=1
print()
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
-
demo05 赌场游戏
1.欢迎进入游戏 2.初始化用户名, 金币为0 3.提示购买金币(100元30个, 充值100的整数倍, 充值不成功可以再次充值) 4.玩一局扣两个币, 猜大小 5.猜对了奖励1个币, 猜错扣1个(可以选择退出或者继续,)
import random
print('*'*20)
print('欢迎进入游戏')
print('*'*20)
username=input('请输入用户名:')
coin=0
play_game=False
print('{}您好,请您充值'.format(username))
while coin<=2:
money=int(input('请输入金额(100的整数倍):'))
if(money%100!=0 or money<100):
print('金额输入有误')
break
else:
coin=int(money/100)*30
play_game=True
exit_game=False
coin-=2
while play_game:
if coin <=2:
print('{}! 您好,您的金币不够了, 请充值后继续'.format(username))
break
elif exit_game:
print('{}! 您好,欢迎下次再来'.format(username))
break;
print('{}!您好, 已进入游戏, 目前剩余{}枚金币'.format(username,coin))
guess=input('请猜大小(d/x)')
t1 = random.randint(1,7)
t2 = random.randint(1,7)
if((t1+t2) > 6 and guess == 'd' or (t1+t2) <= 6 and guess == 'x'):
coin+=1
print('猜对啦!,金币还有{}枚'.format(coin))
else:
coin-=1
print('猜错啦!,金币还有{}枚'.format(coin))
continue_game=input('是否继续本局游戏(y/n):')
if continue_game != 'y':
print('您已退出!欢迎下次再来!!!')
exit_game=True
break
********************
欢迎进入游戏
********************
请输入用户名:lucy
lucy您好,请您充值
请输入金额(100的整数倍):100
lucy!您好, 已进入有戏, 目前剩余28枚金币
请猜大小(d/x)d
猜对啦!,金币还有29枚
是否继续本局游戏(y/n):y
lucy!您好, 已进入有戏, 目前剩余29枚金币
请猜大小(d/x)d
猜错啦!,金币还有28枚
是否继续本局游戏(y/n):n
您已退出!欢迎下次再来!!!