How To Write A Coin Flipping Program On Python?

Asked 2 years ago
Answer 1
Viewed 301
1

You and your brother arguing over who should do the laundry today? Toss a coin. Don't have a room? So make one! This Wikihow article teaches you how to write a Python program that tosses an imaginary digital coin and gives you an idea of what it's like to program. All you need is a Windows computer.

Once you install python, Here is a shortcut to the website https://www.python.org/downloads/ . Press the "Download Python 3.9.1" Option

Writing the Coin Tossing Program 

Go to the start menu and type "IDLE Python". Open this file. This will open IDLE Python.

Image titled PythonStep3.png

 

Press CTRL + N or go to File then New Window to enter Python scripting mode. This is used to write the program.

Image titled PythonStep4.png

Write "random import" on the first line, then type. This will import the random module, which will give access to one of the "random" modules that we are going to use.

Type "print("Welcome to the Coin Flipping program")". This will welcome the user into the program.

 

Type this line: "choice=input("Enter your side (heads or tails): ")" and press Enter. This prompts the user to enter heads or tails. . This transforms whatever the user types into the "choice" variable.

Image titled PythonStep7.png

Image titled PythonStep8.png

Type num=random.randint(1,2) and press Enter. This will randomly import the numbers 2, one and two. Python randomly chooses "num" to become one or two.

 

Create an if statement. You must create a "Result" variable. If the randomly generated number is one, the result is heads. However, if the random number is 2, the result is "Numbers".

Image titled PythonStep9.png

Type "if num==1:" then press Enter (Python will indent you automatically). After the indent, write " result="heads" ". You have to be attentive and write very carefully. This step occurs when the "num" variable is 1.

 

Type "elif num==2:" a new line then press Enter (Python will indent it automatically); by the type of indentation in "result="tails"". This new if statement applies when the variable num is 2. Again, you have to be very careful and type carefully.

 

Create a line that compares the user input and the result variable. To do this, you use a different if statement.

Image titled PythonStep11.png

Start on the next line and type "if choice==result:" then type (Python will indent); In the new line, type "print("Well done, the throw won", Result)". If the user input matches the result, the output is: "Nice job, you win; coin flipped, x" (x is heads or tails).

Image titled PythonStep12.png

Start on the next line and type "else:" then press Enter (Python will indent it); On the new line, write "print("Oh... You lost. The coin was flipped", result)". If the user input does not match the result, it returns: "Oh... you lost. The coin was flipped, 'x' (x is heads or tails).

Image titled PythonStep13.png

Type "print("Thanks for playing. Goodbye")". This informs the user that the program has exited.

 

 

Read Also : What is the audition process for America's Got Talent 2023?
Answered 2 years ago Karl  JablonskiKarl Jablonski