Python Forum
Need help with a mock paper question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with a mock paper question
#1
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.
Reply
#2
What have you tried?
Reply
#3
(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))
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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 !!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020