How to Write a Coin Flipping Program on Python

Download ArticleDownload Article

Are you and your brother debating on who should wash the laundry today? Flip a coin. You don't have a coin? Make one then! This Wikihow article will teach you how to write a program in Python that will flip a digital, imaginary coin and gives you a taste of what's it like to code. All you will need is a computer running Windows.

Part 1
Part 1 of 3:

Downloading Python

Download Article
  1. How.com.vn English: PythonStep1.png
    1
    Go to the Python website and download python. Here is a shortcut to the website https://www.python.org/downloads/ . Press the "Download Python 3.9.1" Option
  2. How.com.vn English: PythonStep2.png
    2
    Save the file to your desktop.
    Advertisement
  3. 3
    Open it.
  4. 4
    Install the Python Program. Once the programme has finished installing, Python should be ready to use on your computer.
  5. Advertisement
Part 2
Part 2 of 3:

Writing the Coin Tossing Program

Download Article
  1. How.com.vn English: PythonStep3.png
    1
    Go to the start menu and type in "IDLE Python". Open that file. This will open up IDLE Python.
  2. How.com.vn English: PythonStep4.png
    2
    Press "CTRL" + "N" or navigate to 'File' then 'New Window' to access Python Scripting Mode. This is what is used to write the program
  3. How.com.vn English: PythonStep5.png
    3
    Type in "import random" on the first line hit then enter. This will import the random module which gives access to one of the "random" modules we will use.
  4. How.com.vn English: PythonStep6.png
    4
    Type in "print ( "Welcome to the Coin Flipping Program")". This will welcome the user to the program.
  5. How.com.vn English: PythonStep7.png
    5
    Type this line " choice=input("Enter your side (heads or tails): ") " and press enter. This tells the user to type in either heads or tails. . This will make whatever the user types will turn into the "choice" variable.
  6. How.com.vn English: PythonStep8.png
    6
    Type in " num=random.randint(1,2) " and press enter. This will randomly import a 2 number, One and Two. Python will randomly choose 'num' to become either one or two.
  7. 7
    Create an if statement. You will need to make a 'result' variable. 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".
  8. How.com.vn English: PythonStep9.png
    8
    Type in " if num==1: ", then press enter (Python will automatically make an indentation); after the indentation type in " result="heads" ". You will need to pay close attention and type very carefully. This step is if the 'num' variable is 1.
  9. How.com.vn English: PythonStep10.png
    9
    Type in " elif num==2: ", one a new line, then press enter (Python will automatically make an indentation); after the indentation type in "result="tails" ". This new if-statement is for if the 'num' variable is 2. Again, you will need to pay close attention and type carefully.
  10. 10
    Make a line that compares the user's input and the 'result' variable. To do this, you will use another if-statement.
  11. How.com.vn English: PythonStep11.png
    11
    Start on the next line and type in " if choice==result: " and then enter (Python will make an indentation); on the new line type in " print("Good Job You won The coin flipped ",result) ". If the user's input is the same as the result, it will print "Good Job You won; The coin flipped, x" (x being either heads or tails).
  12. How.com.vn English: PythonStep12.png
    12
    Start on the next line and type in " else: " and then hit enter (Python will make an indentation); on the new line is, type in " print("Aw... You lost. The coin flipped ", result) ". If the user's input is not the same as the result, it will print "Aw... You lost. The coin flipped ", x" (x being either heads or tails).
  13. How.com.vn English: PythonStep13.png
    13
    Type in " print("Thanks for playing. Goodbye") ". This will let the user know the program is over.
  14. Advertisement
Part 3
Part 3 of 3:

Using the Program

Download Article
  1. How.com.vn English: PythonStep14.png
    1
    Run the program. To do this, you need to save the file by Pressing CTRL+ "S". A file-saver pop-up will show. Save the file on your desktop with any name.
  2. How.com.vn English: PythonStep15.png
    2
    Press "F5" on the top row of your keyboard or navigate to 'Run' and press 'Run Module'. This will run your program.
  3. How.com.vn English: PythonStep16.png
    3
    Type in either "heads" or "tails" (depending on your choice) and press Enter.
  4. How.com.vn English: PythonStep17.png
    4
    View the output. The program will show if you won or not. It will also tell you which side of the imaginary coin it landed on.
  5. Advertisement

Community Q&A

Search
Add New Question
  • Question
    IDLE says 'invalid syntax' because of the 'elif'. What should I do?
    How.com.vn English: JazzTGB145
    JazzTGB145
    Community Answer
    You can use 'else:' Statement Instead of using the 'elif:' Statement. Note: Don't use any conditions like 'else num == result:' Just use 'else:'
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
      Advertisement

      Tips

      1. Pay very close attention to symbols such as commas, parentheses, colons, and apostrophes.
      2. Pay very close attention to when you should start a new line and when you should make an indentation.
      3. When running your code, if you come across a syntax error, it might be because you misspelled something.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      Things You'll Need

      1. A computer running Windows.

      About This Article

      How.com.vn is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time. This article has been viewed 22,312 times.
      How helpful is this?
      Co-authors: 3
      Updated: February 23, 2021
      Views: 22,312
      Categories: Python
      Thanks to all authors for creating a page that has been read 22,312 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement