# Python学习之Craps赌博游戏篇

Craps赌博游戏：

 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('你破产了, 游戏结束!')

 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('你已经破产，游戏结束！')

posted @ 2019-05-18 17:52 极限之旅 阅读(...) 评论(...) 编辑 收藏