Tuesday, June 25, 2019

Make a program that flips a coin in Python 3

Welcome back to another tutorial, today we will be making a script that flips a coin and randomly selects heads or tails.

To start, make a new python file and name it CoinFlip, once you’ve done that you will need to import the random module. Add the following to the first line of code:

import random

Once you’ve done that you will want to make a string named “coin” and set it equal to a list that includes, “heads” and “tails”. the string should look something like this:

coin = [‘heads’, ‘tails’]

Once you have completed that step only two more to go. You should now be able to print the string coin”
but before you do that you need to make the program select one of the options in the list, to do this enter
the following code:

print(random.choice(coin))

Once that is put into the code, you will now be able to run it and get heads or tails, feel free to run it as
much as you like to make sure it’s random.

I hope you enjoyed today’s tutorial, stay tuned for some more awesome tutorials.

All Code:
import random
coin = [‘heads’, ‘tails’]
print(random.choice(coin))

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.