-1

I have one problem with understanding Object-Oriented Programming. Why I got:

NameError: name 'Coin' is not defined?

And could you please explain me how Classes and Objects work in this program. I don't understand why we should pass self parameter.

import random  # The Coin class simulates 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 random number
    # in the range of 0 through 1. If the number
    # is 0, then sideup is set to 'Heads'. # Otherwise, sideup is 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. 32

    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 ()
Dale K
  • 25,246
  • 15
  • 42
  • 71
  • You need `main` function definition as well as the call of it (the last line) unindented one line – buran Oct 03 '19 at 08:14

2 Answers2

0

considering the NameError:

import random  # The Coin class simulates 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 random number
    # in the range of 0 through 1. If the number
    # is 0, then sideup is set to 'Heads'. # Otherwise, sideup is 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. 32

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 ()

Note how the main function definition is unindented a level. You had it as part of your class. Also the function call main() needed to be unindented bto the same level.

As for your questions about OOP and the self-keyword, i suggest you take a look at this question and the documentation.

In short: for the class methods to be able to modify an object's attributes, it needs to know which object those attributes belong to. It needs a reference to the object it applies to. self is this reference. It tells the object: "this is about me!". fun fact: You don't need to call it self; you can give it any name!

Kraay89
  • 919
  • 1
  • 7
  • 19
-1

it is simple usage of class and objects :

class Shark:
    def swim(self):
        print("The shark is swimming.")

    def be_awesome(self):
        print("The shark is being awesome.")


def main():
    sammy = Shark()
    sammy.swim()
    sammy.be_awesome()

if __name__ == "__main__":
    main()
nimajv
  • 423
  • 3
  • 11