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"

def

play

(

pred

)

:

# Convert prediction into a binary star list

with

'Tails'

=

0

,

'Heads'

=

1

pred

=

[

1

if

one

==

'H'

else

0

for

iodine

in

pred

]

# coin impudent pretense def

flip

(

)

:

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!'

print

(

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 .

 def 

check

(

x

)

:

if

len

(

x

)

!=

3

:

return

False check

=

[

]

for

one

in

ten

:

check

.

append

(

one

in

[

't'

,

'T'

,

'h'

,

'H'

]

)

return

check

[

0

]

and check

[

1

]

and check

[

2

]

and

len

(

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 

=

True

while

replay

==

True

:

pred

=

input

(

'\nEnter your prediction in the shape "XXX" where X is either T or H\n'

)

if

not

check

(

pred

)

:

raise

ValueError

(

'Input must be of shape "XXX" where X is either T or H either lower or uppercase\n'

)

break

play

(

pred

)

repeat

=

''

while

(

reprise not

in

[

'y'

,

'Y'

]

and repeat not

in

[

'q'

,

'Q'

]

)

:

repeat

=

input

(

'\nPress y to play again or q to quit\n'

)

if

repeat

in

[

'q'

,

'Q'

]

:

print

(

"Thanks for playing \nGood Bye!!!"

)

replay

=

recur

in

[

'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 prediction 

in

the shape

"XXX"

where

X

is either

T

or

H

TTT

Computer won after

10

flips

!

Press yttrium to play again or q to quit y Enter your prediction

in

the shape

"XXX"

where

X

is either

T

or

H

hhh Congratulations

,

you won after

3

flips

!

Press y to play again or q to quit q Thanks

for

playing Good Bye

!

!

!

  • In the second test I entered a wrong prediction format and got an error as expected .
 record your prediction 

in

the shape

"XXX"

where

X

is either

T

or

H

tHG

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

--

-

ValueError

Traceback

(

most recent call last

)

<

ipython

-

input

-

95

-

75

ccb25d76e0

>

in

<

module

>

5

6

if

not

check

(

pred

)

:

--

--

>

7

raise

ValueError

(

'Input must be of shape "XXX" where X is either T or H either lower or uppercase\n'

)

8

break

9

ValueError

:

Input must be

of

shape

"XXX"

where

X

is either

T

or

H

either lower or uppercase

Find the code in this github depository

source : https://ontopwiki.com
Category : Finance

Post navigation

Leave a Comment

Trả lời

Email của bạn sẽ không được hiển thị công khai.