Tossing of a coin Heads up or Tails up
Write a program to represent the tossing of a coin. In the program we need to repeatedly toss the coin and each time determine whether it landed heads up or tails up. Taking an object-oriented approach, we will write a class named Cointoss that can perform the behaviors of the coin.
## #Python's program to toss the coin and determine heads up or tails up. ## #import import random #Cointoss class to simulates the coin that can be flipped class Cointoss: #The _ _init_ _ method initializes the upperside data attribute with 'Tails'. def __init__(self): self.upperside = 'Tails' #The tossing method generates a random number 1 and 0 using randint function. def tossing(self): if random.randint(0,1) == 0: self.upperside = 'Tails' else: self.upperside = 'Heads' #The get_upperside method returns the value referenced by upperside. def get_upperside(self): return self.upperside def main(): # Create an object from the Cointoss class. start_toss = Cointoss() # Object calling the function of class. print("This is upper side Before Toss :[", start_toss.get_upperside(),"]") print("........tossing the coin........") start_toss.tossing() # Object calling the function of class. print("Got the up side After Toss :[",start_toss.get_upperside(),"]") # Call the main function. main()
Sample output of above program.
C:\Python\programs>python program.py
This is upper side Before Toss :[ Tails ]
........tossing the coin........
Got the up side After Toss :[ Tails ]
C:\Python\programs>python program.py
This is upper side Before Toss :[ Tails ]
........tossing the coin........
Got the up side After Toss :[ Tails ]
C:\Python\programs>python program.py
This is upper side Before Toss :[ Tails ]
........tossing the coin........
Got the up side After Toss :[ Heads ]
C:\Python\programs>python program.py
This is upper side Before Toss :[ Tails ]
........tossing the coin........
Got the up side After Toss :[ Heads ]
C:\Python\programs>python program.py
This is upper side Before Toss :[ Tails ]
........tossing the coin........
Got the up side After Toss :[ Tails ]
C:\Python\programs>
This is upper side Before Toss :[ Tails ]
........tossing the coin........
Got the up side After Toss :[ Tails ]
C:\Python\programs>python program.py
This is upper side Before Toss :[ Tails ]
........tossing the coin........
Got the up side After Toss :[ Tails ]
C:\Python\programs>python program.py
This is upper side Before Toss :[ Tails ]
........tossing the coin........
Got the up side After Toss :[ Heads ]
C:\Python\programs>python program.py
This is upper side Before Toss :[ Tails ]
........tossing the coin........
Got the up side After Toss :[ Heads ]
C:\Python\programs>python program.py
This is upper side Before Toss :[ Tails ]
........tossing the coin........
Got the up side After Toss :[ Tails ]
C:\Python\programs>
2017-09-21T13:38:13+05:30
2017-09-21T13:38:13+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution