-
Notifications
You must be signed in to change notification settings - Fork 0
/
21game.py
61 lines (54 loc) · 1.22 KB
/
21game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import random
state=[]
def win():
print("CONGRATULATIONS YOU WIN!!")
exit()
def lose():
print("YOU LOSE!!")
exit()
def check():
#print("current state ->",state)
length=len(state)
for i in range(1,length+1):
if i!=state[i-1] or length>=21:
return False
return True
def comp():
last=len(state)
if last==20:
win()
elif last<18:
num=random.randint(1,3)
for i in range(1,num+1):
state.append(last+i)
else:
for i in range(1,21-last):
state.append(i+last)
if check()==False:
win()
def player():
print("current state ->",state)
num=int(input("how many numbers would you like to enter 1-3\n> "))
if num<=3 and num>0:
for i in range(1,num+1):
state.append(int(input(f"input {i} > ")))
check()
else :
print("Invalid input")
lose()
if check()==False:
lose()
def start():
print("1-play first?")
print("2-play second?")
play=int(input("> "))
if play==1:
while True:
player()
comp()
else:
while True:
comp()
player()
if __name__=="__main__":
start()