-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.py
147 lines (131 loc) · 4.98 KB
/
21.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import random
# returns the nearest multiple to 4
def nearestMultiple(num):
if num >= 4:
near = num + (4 - (num % 4))
else:
near = 4
return near
def lose1():
print("\n\nYOU LOSE !")
print("Better luck next time !")
return True
# checks whether the numbers are consecutive
def check(xyz):
i = 1
while i < len(xyz):
if (xyz[i] - xyz[i-1]) != 1:
return False
i = i + 1
return True
# starts the game
def start1():
xyz = []
last = 0
while True:
print("Enter 'F' to take the first chance.")
print("Enter 'S' to take the second chance.")
chance = input('> ')
# player takes the first chance
if chance.lower() == "f":
while True:
if last == 21:
return lose1()
print("\nYour Turn.")
print("\nHow many numbers do you wish to enter (1-4)?")
inp = int(input('> '))
if inp > 0 and inp <= 4:
comp = random.randint(1, 4) # Randomly choose the number of numbers for the computer
i = 1
print("Now enter the values")
while i <= inp:
try:
a = int(input('> '))
xyz.append(a)
i = i + 1
except ValueError:
print("Invalid input. Please enter an integer.")
# store the last element of xyz.
last = xyz[-1]
# checks whether the input numbers are consecutive
if check(xyz) == True:
if last == 21:
return lose1()
else:
# "Computer's turn."
j = 1
while j <= comp:
xyz.append(last + j)
j = j + 1
print("Order of inputs after computer's turn is: ")
print(xyz)
last = xyz[-1]
else:
print("\nYou did not input consecutive integers.")
return True
else:
print("Invalid input. You can enter 1 to 4 numbers at a time.")
return True
# player takes the second chance
elif chance.lower() == "s":
comp = random.randint(1, 4) # Randomly choose the number of numbers for the computer
last = 0
while last < 21:
# "Computer's turn"
j = 1
while j <= comp:
xyz.append(last + j)
j = j + 1
print("Order of inputs after computer's turn is:")
print(xyz)
if xyz[-1] == 21:
return lose1()
else:
print("\nYour turn.")
print("\nHow many numbers do you wish to enter (1-4)?")
inp = input('> ')
try:
inp = int(inp)
except ValueError:
print("Invalid input. Please enter an integer.")
return True
i = 1
print("Enter your values")
while i <= inp:
try:
xyz.append(int(input('> ')))
i = i + 1
except ValueError:
print("Invalid input. Please enter an integer.")
last = xyz[-1]
if check(xyz) == True:
near = nearestMultiple(last)
comp = random.randint(1, 4) # Randomly choose the number of numbers for the computer
else:
# if inputs are not consecutive
# automatically disqualified
print("\nYou did not input consecutive integers.")
return True
print("\n\nCONGRATULATIONS !!!")
print("YOU WON !")
return True
else:
print("wrong choice")
game = True
while game:
print("Player 2 is Computer.")
print("Do you want to play the 21 number game? (Yes / No)")
ans = input('> ')
if ans.lower() == 'yes':
game = start1()
else:
print("Do you want to quit the game? (yes / no)")
nex = input('> ')
if nex.lower() == "yes":
print("You are quitting the game...")
game = False
break
elif nex.lower() == "no":
print("Continuing...")
else:
print("Wrong choice")