Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 073dcd7

Browse files
committed
写了好几个题,写的最开心的是CRAPS赌博游戏
一开始是玩家一直赢,然后改正了变成庄家开始赢一点点了,改到最后才发现原来庄家还这么能赢
1 parent 41d3865 commit 073dcd7

3 files changed

Lines changed: 178 additions & 0 deletions

File tree

4.95 KB
Binary file not shown.

code/day5.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
#这部分只需要学一下match case这一块
22

3+
status_code=int(input('相应状态码'))
4+
match status_code:
5+
case 400:description = 'bad request'
6+
case 401:description = 'unauthorized'
7+
case 403:description = 'forbidden'
8+
case 404: description = 'Not Found'
9+
case 405: description = 'Method Not Allowed'
10+
case 418: description = 'I am a teapot'
11+
case 429: description = 'Too many requests'
12+
case _: description = 'Unknown Status Code'
13+
print('状态码描述',description)
14+

code/day7.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#循环结构和分支结构实战
2+
3+
#素数判断
4+
def prime_judge():
5+
x=int (input('输入数字'))
6+
is_prime=True#是素数
7+
if x<=1:
8+
is_prime =False
9+
elif x==2:
10+
is_prime=True
11+
else :
12+
if x % 2==0:
13+
is_prime = False
14+
else:
15+
for i in range(3, x, 2):
16+
if x % i ==0:
17+
is_prime=False
18+
19+
if is_prime==True:
20+
print ('is prime')
21+
else:
22+
print ('not a prime')
23+
24+
#斐波那契数列
25+
def Fibonacci_sequence():
26+
a ,b =0 ,1
27+
for _ in range(20):
28+
a, b =b, a+b
29+
print(a)
30+
31+
32+
#水仙花数_通用版
33+
def narcissistic_number():
34+
a =int (input('输入数字判断是否是水仙花数'))
35+
digits=[]
36+
org =a
37+
if a==0:
38+
print('是水仙花数')
39+
else:
40+
while a >0:
41+
digit=a % 10
42+
digits.append(digit)
43+
a //= 10
44+
n =len(digits)
45+
total = 0
46+
for i in range(n):
47+
total += digits[i]**n
48+
49+
if total ==org:
50+
print('是水仙花数')
51+
else:
52+
print('不是水仙花数')
53+
54+
#百钱百鸡问题
55+
def c100cny_100chick():
56+
for i in range(101):
57+
for j in range (101):
58+
for k in range (101):
59+
if i+j+k==100 and 5*i +3*j +k/3 ==100 :
60+
print(f'公鸡: {i}只, 母鸡: {j}只, 小鸡: {k}只')
61+
62+
#百钱百鸡问题优化版
63+
def c100cny_100chick_2():
64+
for x in range(0, 21):
65+
for y in range(0, 34):
66+
z = 100 - x - y
67+
if z % 3 == 0 and 5 * x + 3 * y + z // 3 == 100:
68+
print(f'公鸡: {x}只, 母鸡: {y}只, 小鸡: {z}只')
69+
70+
#CRAPS赌博游戏
71+
def CRAPS():
72+
import random
73+
74+
money =1000
75+
print ('这是一个简单的Python小游戏 \n CRAPS又称花旗骰,是美国拉斯维加斯非常受欢迎的一种的桌上赌博游戏。 \n 该游戏使用两粒骰子,玩家通过摇两粒骰子获得点数进行游戏。简化后的规则是:玩家第一次摇骰子如果摇出了 7 点或 11 点,玩家胜;玩家第一次如果摇出 2 点、3 点或 12 点,庄家胜;玩家如果摇出其他点数则游戏继续,玩家重新摇骰子,如果玩家摇出了 7 点,庄家胜;如果玩家摇出了第一次摇的点数,玩家胜;其他点数玩家继续摇骰子,直到分出胜负。')
76+
while money >0:
77+
while True:
78+
try :
79+
debt= int (input('请输入赌注'))
80+
except ValueError :
81+
print('请输入整数')
82+
continue
83+
84+
if 0 < debt <= money :
85+
break
86+
else:
87+
if debt <= 0:
88+
print('赌注不能小于或等于0')
89+
elif debt > money:
90+
print('赌注不能多于现有资产')
91+
92+
continue
93+
94+
while True:
95+
shake1= random.randrange(1,7)
96+
shake2= random.randrange(1,7)
97+
shake_1 =shake1 +shake2
98+
if shake_1 ==7 or shake_1 == 11:
99+
print ('玩家胜')
100+
money +=debt
101+
print(f'当前剩余{money}')
102+
break
103+
elif shake_1==2 or shake_1 == 3 or shake_1 ==12:
104+
print ('庄家胜')
105+
money -=debt
106+
print(f'当前剩余{money}')
107+
break
108+
else:
109+
while True:
110+
shake3= random.randrange(1,7)
111+
shake4= random.randrange(1,7)
112+
shake_2 =shake3 +shake4
113+
114+
if shake_2 == 7:
115+
print ('庄家胜')
116+
money -= debt
117+
print(f'当前剩余{money}')
118+
break
119+
elif shake_1 == shake_2:
120+
print ('玩家胜')
121+
money +=debt
122+
print(f'当前剩余{money}')
123+
break
124+
else :
125+
continue
126+
break
127+
128+
print('输光了赌注')
129+
130+
131+
def run():
132+
while True:
133+
print('1. 素数判断')
134+
print('2. 斐波那契数列(打印前20项)')
135+
print('3. 水仙花数判断')
136+
print('4. 百钱百鸡问题')
137+
print('5. 百钱百鸡问题(优化版)')
138+
print('6. CRAPS赌博游戏')
139+
print('0. 退出')
140+
try:
141+
m = int(input('请选择运行:'))
142+
except ValueError:
143+
print('请输入数字(如:1 / 2 / 3 / 4 / 5 / 6 / 0)')
144+
continue
145+
146+
match m:
147+
case 1:
148+
prime_judge()
149+
case 2:
150+
Fibonacci_sequence()
151+
case 3:
152+
narcissistic_number()
153+
case 4:
154+
c100cny_100chick()
155+
case 5:
156+
c100cny_100chick_2()
157+
case 6:
158+
CRAPS()
159+
case 0:
160+
print('退出')
161+
break
162+
case _:
163+
print('输入有误,请重新选择')
164+
165+
if __name__ == "__main__":
166+
run()

0 commit comments

Comments
 (0)