Reading: Coin Toss Game using Python
In this plan, I will show you how to implement a simple mint pass game in python. The post is divided in three main part. First I will explain the game rules, then the python execution of the game and ultimately I will perform some tests.
1. Game rules
This game is played by a single exploiter against the computer. The actor predicts the consequence of three consecutive coin tosses, for case THH where H stands for heads and T stands for tails. Behind the scenes, the calculator makes its own prediction based on that of the user. The coin is then flipped until the last three straight coin tosses match either predictions. The musician wins in the case his/her prediction is the one obtained, otherwise the computer wins. In either sheath, the musician is given the luck to retry.
2. Code Explanation
The code is divided into three main pieces, two functions and the main play loop. A sow of 2021 has been set to make certain that the randomness is “ reproducible ”.
a. The serve play ( )
- The first function play, takes in the player ‘s prediction as a string ( case insensitive, that is ‘ttt ‘ = ‘TTT ‘ ) and converts it into a list of binary values : 1 for ‘Heads ‘ and 0 for ‘Tails ‘ .
- There ‘s a nest function pass with no controversy, that returns randomly 0 or 1, to simulate a coin pass .
- The computer prediction is derived from the player ‘s as follows : if musician ‘s prediction is “ X1-X2-X3 ” then calculator = “ not ( X2 ) -X1-X1 ” .
- The coin is then flipped, the leave is appended to the variable result and the final three back-to-back values are compared with both predictions, until either matches. The achiever is announced by print ( achiever ) along with the number of coin tosses executed, and the officiate returns nothing .
import
random random.
seed
(
2021
)
# ensure randomness"reproducibility"
defplay
(
pred)
:
# Convert prediction into a binary star listwith
'Tails'
=
0
,
'Heads'
=
1
pred=
[
1
if
one==
'H'
else
0
for
iodinein
pred]
# coin impudent pretense defflip
(
)
:
return
random.
randint
(
0
,
2
)
computer=
[
int
(
not pred[
1
]
)
,
pred[
0
]
,
pred[
0
]
]
result=
[
]
while
outcome[
-
3
:
]
!=
calculator and consequence[
-
3
:
]
!=
pred:
result.
append
(
flip
(
)
)
if
outcome[
-
3
:
]
==
pred:
achiever=
'Congratulations, you won after '
+
str
(
len
(
consequence)
)
+
' flips!'
else
:
achiever=
'Computer won after '
+
str
(
len
(
consequence)
)
+
' flips!'
(
winner)
return
b. The error handling function check()
It takes the drug user ‘s prediction as controversy and returns True if it has the right condition and contains the proper letters, otherwise it returns False .
defcheck
(
x)
:
if
len
(
x)
!=
3
:
return
False check=
[
]
for
onein
ten:
check.
append
(
onein
[
't'
,
'T'
,
'h'
,
'H'
]
)
return
check[
0
]
and check[
1
]
and check[
Read more: Mini Coin Purse – Free Crochet Pattern
2
]
andlen
(
x)
==
3
c. The main playing while loop
here the player is asked to enter his/her prediction then this prediction is checked. The game continues only if the shape and letters of prediction are chastise otherwise, an error message is thrown. One gambling round is performed, and the achiever is prompted to select whether to play another round or to quit. The inner while loop makes helps make sure that the plot continues until the player decides to quit. The decision of the player to continue ( y ) or to quit ( q ) is lawsuit insensitive .
replay=
Truewhile
replay==
True:
pred=
input
(
'\nEnter your prediction in the shape "XXX" where X is either T or H\n'
)
if
notcheck
(
pred)
:
raiseValueError
(
'Input must be of shape "XXX" where X is either T or H either lower or uppercase\n'
)
break
play
(
pred)
repeat=
''
while
(
reprise notin
[
'y'
,
'Y'
]
and repeat notin
[
'q'
,
'Q'
]
)
:
repeat=
input
(
'\nPress y to play again or q to quit\n'
)
if
repeatin
[
'q'
,
'Q'
]
:
(
"Thanks for playing \nGood Bye!!!"
)
replay=
recurin
[
'y'
,
'Y'
]
3. Game tests
here are the results of three tests I performed on the plot :
- here I won in the second attack after 3 mint tosses .
enter your predictionin
the shape"XXX"
whereX
is eitherT
orH
TTT
Computer won after10
flips!
Press yttrium to play again or q to quit y Enter your predictionin
the shape"XXX"
whereX
is eitherT
orH
hhh Congratulations,
you won after3
flips!
Press y to play again or q to quit q Thanksfor
playing Good Bye!
!
!
- In the second test I entered a wrong prediction format and got an error as expected .
record your predictionin
the shape"XXX"
whereX
is eitherT
orH
tHG--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
ValueErrorTraceback
(
most recent call last)
<
ipython-
input-
95
-
75
ccb25d76e0>
in
<
module>
5
6
if
notcheck
(
pred)
:
--
--
>
7
raiseValueError
(
'Input must be of shape "XXX" where X is either T or H either lower or uppercase\n'
)
8
break
9
ValueError:
Input must beof
shape"XXX"
whereX
is eitherT
orH
either lower or uppercase
Find the code in this github depository
Leave a Comment