Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.13 KB | None | 0 0
  1. # This program demostrates OOP
  2.  
  3. import random
  4.  
  5. # The Coin class stimulates a coin that can be flipped.
  6.  
  7. class Coin:
  8.     # The __init__ method initializes the sideup data
  9.     # attribute with 'Heads'.
  10.     def __init__(self):
  11.         self.sideup = 'Heads'
  12.  
  13.     # The toss method generates a tandom number in
  14.     # the range of 0 through 1. If the number is 0,
  15.     # then sideup is set to 'Heads'. Otherwise, sideup
  16.     # set to 'Tails'.
  17.     def toss(self):
  18.         if random.randint(0, 1) == 0:
  19.             self.sideup == 'Heads'
  20.         else:
  21.             self.sideup == 'Tails'
  22.  
  23.     # The get_sideup method returns the value referenced
  24.     # by sideup.
  25.     def get_sideup(self):
  26.         return self.sideup
  27.  
  28. # The main function.
  29. def main():
  30.     # Create an object from the Coin class.
  31.     my_coin = Coin()
  32.  
  33.     # Display the side of the coin that is facing up.
  34.     print('This side is up:', my_coin.get_sideup())
  35.  
  36.     # Toss the coin.
  37.     print('I am going to toss the coin ten times:')
  38.     for count in range(10):
  39.         my_coin.toss()
  40.         print(my_coin.get_sideup())
  41.    
  42.  
  43.    
  44. # Call the main function.
  45. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement