Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Coin Toss

Tags:

python

I am VERY new to Python and I have to create a game that simulates flipping a coin and ask the user to enter the number of times that a coin should be tossed. Based on that response the program has to choose a random number that is either 0 or 1 (and decide which represents “heads” and which represents “tails”) for that specified number of times. Count the number of “heads” and the number of “tails” produced, and present the following information to the user: a list consisting of the simulated coin tosses, and a summary of the number of heads and the number of tails produced. For example, if a user enters 5, the coin toss simulation may result in [‘heads’, ‘tails’, ‘tails’, ‘heads’, ‘heads’]. The program should print something like the following: “ [‘heads’, ‘tails’, ‘tails’, ‘heads’, ‘heads’]

This is what I have so far, and it isn't working at all...

import random

def coinToss():
    number = input("Number of times to flip coin: ")
    recordList = []
    heads = 0
    tails = 0
    flip = random.randint(0, 1)
    if (flip == 0):
        print("Heads")
        recordList.append("Heads")
    else:
        print("Tails")
        recordList.append("Tails")
    print(str(recordList))
    print(str(recordList.count("Heads")) + str(recordList.count("Tails")))
like image 958
YeahScience Avatar asked Feb 14 '13 19:02

YeahScience


People also ask

How do you make heads or tails in Python?

Create an if statement. If the randomly generated number is one, then the result will be "heads. But if the random number is 2, then the result will be "tails". Type in " if num==1: ", then press enter (Python will automatically make an indentation); after the indentation type in " result="heads" ".

What is the coin toss method?

The toss or flip of a coin to randomly assign a decision traditionally involves throwing a coin into the air and seeing which side lands facing up. This method may be used to resolve a dispute, see who goes first in a game or determine which type of treatment a patient receives in a clinical trial.

Can you rig a coin toss?

The ubiquitous coin toss is not so random after all, and can easily be manipulated to turn up heads, or tails, a Canadian study has found.


2 Answers

You need a loop to do this. I suggest a for loop:

import random
def coinToss():
    number = input("Number of times to flip coin: ")
    recordList = []
    heads = 0
    tails = 0
    for amount in range(number):
         flip = random.randint(0, 1)
         if (flip == 0):
              print("Heads")
              recordList.append("Heads")
         else:
              print("Tails")
              recordList.append("Tails")
    print(str(recordList))
    print(str(recordList.count("Heads")) + str(recordList.count("Tails")))

I suggest you read this on for loops.

Also, you could pass number as a parameter to the function:

import random
def coinToss(number):
    recordList, heads, tails = [], 0, 0 # multiple assignment
    for i in range(number): # do this 'number' amount of times
         flip = random.randint(0, 1)
         if (flip == 0):
              print("Heads")
              recordList.append("Heads")
         else:
              print("Tails")
              recordList.append("Tails")
    print(str(recordList))
    print(str(recordList.count("Heads")) + str(recordList.count("Tails")))

Then, you need to call the function in the end: coinToss().

like image 77
Rushy Panchal Avatar answered Sep 19 '22 17:09

Rushy Panchal


You are nearly there:

1) You need to call the function:

coinToss()

2) You need to set up a loop to call random.randint() repeatedly.

like image 24
NPE Avatar answered Sep 22 '22 17:09

NPE