Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This program demostrates OOP
- import random
- # The Coin class stimulates a coin that can be flipped.
- class Coin:
- # The __init__ method initializes the sideup data
- # attribute with 'Heads'.
- def __init__(self):
- self.sideup = 'Heads'
- # The toss method generates a tandom number in
- # the range of 0 through 1. If the number is 0,
- # then sideup is set to 'Heads'. Otherwise, sideup
- # set to 'Tails'.
- def toss(self):
- if random.randint(0, 1) == 0:
- self.sideup == 'Heads'
- else:
- self.sideup == 'Tails'
- # The get_sideup method returns the value referenced
- # by sideup.
- def get_sideup(self):
- return self.sideup
- # The main function.
- def main():
- # Create an object from the Coin class.
- my_coin = Coin()
- # Display the side of the coin that is facing up.
- print('This side is up:', my_coin.get_sideup())
- # Toss the coin.
- print('I am going to toss the coin ten times:')
- for count in range(10):
- my_coin.toss()
- print(my_coin.get_sideup())
- # Call the main function.
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement