Python Forum

Full Version: Need help with a mock paper question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Q. Your friend has devised a game with two players. The two players,
called A and B, take turns rolling an ordinary six-sided die, with A being
the first to roll.
The first player who rolls a six wins the game.
You and your friend do not agree about what the probability of A winning
the game is, and you therefore decide to simulate the game with a
computer
Thus: write a Python program that performs 10 trials, each consisting of
10000 games, and for each trial prints the fraction of the games won by
player A.
What have you tried?
(Dec-05-2018, 06:00 PM)Larz60+ Wrote: [ -> ]What have you tried?
#this is what I have so far program still not working out.
import random

def rollDie(numTrials):
  return random.choice([1,2,3,4,5,6])

def playGame(numTrials):
  winsA, winsB = 0, 0
  throwA, throwB = 0, 0
  for j in range(1000):
    haveWon = False
    while haveWon == False:
      throwA = rollDie()
      if throwA == 6:
        haveWon = True
        winsA += 1
        break
      else:
        throwB == rollDie()
        if throwB == 6:
            haveWon == True
            winsB += 1
            break
print(winsA, winsB, winsA/(numTrials*1000),winsB/(numTrials*1000))
Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time.

You never call you function, and your function never returns anything so you can make use of the information in the final print statement. See the function tutorial on how to call functions and return values from them.
(Dec-07-2018, 06:45 PM)ichabod801 Wrote: [ -> ]Please use python and output tags when posting code and results. I put them in for you this time. Here are instructions for doing it yourself next time. You never call you function, and your function never returns anything so you can make use of the information in the final print statement. See the function tutorial on how to call functions and return values from them.
Thanks for your help !!