在此先安利一波大佬的Python学习项目地址: https://github.com/jackfrued/Python-100-Days
这些天一直在看着大佬的项目学习Python,这是第五天循环学习完的练习,感觉很有意思,就照着意思写了一下,期间也改进了很多不满意的地方,个人认为这个例子真的很。
可以根据运行中产生的很多问题一步步去解决,达到自己理想的效果
Craps赌博游戏:
Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python。
玩家摇两颗色子 如果第一次摇出7点或11点 玩家胜
如果摇出2点 3点 12点 庄家胜 其他情况游戏继续
玩家再次要色子 如果摇出7点 庄家胜
如果摇出第一次摇的点数 玩家胜
否则游戏继续 玩家继续摇色子
玩家进入游戏时有1000元的赌注 全部输光游戏结束
这是那位大佬的版本:
1 from random import randint 2 3 money = 1000 4 while money > 0: 5 print('你的总资产为:', money) 6 needs_go_on = False 7 while True: 8 debt = int(input('请下注: ')) 9 if debt > 0 and debt <= money: 10 break 11 first = randint(1, 6) + randint(1, 6) 12 print('玩家摇出了%d点' % first) 13 if first == 7 or first == 11: 14 print('玩家胜!') 15 money += debt 16 elif first == 2 or first == 3 or first == 12: 17 print('庄家胜!') 18 money -= debt 19 else: 20 needs_go_on = True 21 22 while needs_go_on: 23 current = randint(1, 6) + randint(1, 6) 24 print('玩家摇出了%d点' % current) 25 if current == 7: 26 print('庄家胜') 27 money -= debt 28 needs_go_on = False 29 elif current == first: 30 print('玩家胜') 31 money += debt 32 needs_go_on = False 33 34 print('你破产了, 游戏结束!') 大佬的版本我觉得只有一个不友好的地方就是当输入为空时,会出现一个ValueError的异常,这样我认为不太友好,所以改版了一下,下面是我的版本
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> 1 from random import randint
2 money=1000
3 while money>0:
4 touzhu=0
5 try:
6 touzhu = int(input('你投注的金额是:'))
7 except ValueError:
8 print('输入为空',end=', ')
9 #touzhu=int(input())
10 finally:
11 flag=False
12 if touzhu <= 0:
13 print('投注无效,请重新投注')
14 elif touzhu > money:
15 print('投注超过了你的资产总和,请重新投注')
16 else:
17 shuzi=randint(1,6)+randint(1,6)
18 #print('本次摇出的点数为:%d'%shuzi)
19 cun=shuzi
20 if shuzi==7 or shuzi==11:
21 print('恭喜,你赢了!')
22 money+=touzhu
23 elif shuzi==2 or shuzi==3 or shuzi==12:
24 print('不好意思,你输了!')
25 #zhuangjia+=touzhu
26 money-=touzhu
27 else:
28 flag=True
29 zongtouzhu = 0
30 while flag:
31 jiazhu=-1
32 try:
33 jiazhu=int(input('你的加注金额是:'))
34 except ValueError:
35 print('输入为空',end=', ')
36 finally:
37 zongtouzhu+=jiazhu
38 flag=False
39 if zongtouzhu+touzhu > money:
40 print('加注超过了你的资产总和,请重新加注')
41 zongtouzhu-=jiazhu
42 flag=True
43 elif jiazhu < 0:
44 #可以不加注,则加注为0
45 print('加注无效,请重新加注')
46 flag=True
47 zongtouzhu-=jiazhu
48 else:
49 shuzi=randint(1,6)+randint(1,6)
50 #print('本次摇出的点数为:%d'%(shuzi))
51 if shuzi==7:
52 print('不好意思,你输了!')
53 money-=(touzhu+zongtouzhu)
54 #zhuangjia+=touzhu
55 elif shuzi==cun:
56 print('恭喜,你赢了!')
57 money+=(touzhu+zongtouzhu)
58 else:
59 flag=True
60 #print(money)
61 print('你已经破产,游戏结束!')
</pre>
<input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
本来按照游戏原本的思路去写的,写着写着就想要不第二次以后摇色子来个加注吧,这样更刺激嘛,哈哈哈哈,另这个版本当输入为空时,提示为无效,可以重新投注,游戏继续,
不会出现红色异常的,其他就没什么改进了,想加个界面做成一个完整的摇色子游戏,可是这块还不会,继续加油了
网友评论