Python Forum
Creating class - name not defined error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating class - name not defined error
#1
Hello, I am trying to learn how to create a class by following the toss coin exercise from a textbook but I kept getting the error (NameError: name Coin is not defined). I copied exactly as shown in the texbook and I am using the latest IDLE 3.65. Wonder if anyone could advise me what I did wrong or is it a version compatibility problem. Thanks.
import random

class Coin:

     def __init__(self):
          self.sideup = 'Heads'

     def toss(self):
          if random.ranint(0,1) ==0:
               self.sideup = 'Heads'
          else:
               self.sideup  = 'Tails'

     def get_sideup(self):
          return self.sideup

     def main():
          mycoin = Coin()

          print ('This side is up: ', my_coin.get_sideup())
          print ('I am tossing the coin...')
          my_coin.toss()
          print ('This side is up: ', my_coin.get_sideup())
          
     main()

Noted, thanks.
Reply
#2
Pay attention at indentation of you main() function, it must be outside of the Coin class.

import random


class Coin:

    def __init__(self):
        self.sideup = 'Heads'

    def toss(self):
        # Changed to randrange(interval)
        if random.randrange(2) == 0:
            self.sideup = 'Heads'
        else:
            self.sideup = 'Tails'

    def get_sideup(self):
        return self.sideup


def main():
    # Changed to my_coin, like you call after
    my_coin = Coin()

    print('This side is up: ', my_coin.get_sideup())
    print('I am tossing the coin...')
    my_coin.toss()
    print('This side is up: ', my_coin.get_sideup())


main()
Reply
#3
Smile thank you so much. Was never aware that indent has an effect.
Reply
#4
(Jun-30-2018, 01:30 PM)python_alex Wrote: I am using the latest IDLE 3.65
Can show some small changes,get method removed as is not needed in Python,and trow in f-string as you use 3.6.
I don't like the function name main() i know it's used everywhere in Python,just give a name that fit code better.
import random

class Coin:
    def __init__(self):
        self.sideup = 'Heads'

    def toss(self):
        if random.randrange(2) == 0:
            self.sideup = 'Heads'
        else:
            self.sideup = 'Tails'

def toss_result():
    my_coin = Coin()
    print(f'This side is up: {my_coin.sideup}')
    print('I am tossing the coin...')
    my_coin.toss()
    print(f'This side is up: {my_coin.sideup}')

if __name__ == '__main__':
    toss_result()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 517 Nov-23-2023, 02:53 PM
Last Post: rob101
  "Name is not defined" when running a class lil_e 6 3,770 Jan-12-2023, 11:57 PM
Last Post: lil_e
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,045 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Error 'Contour' not Defined DaveG 3 2,283 Mar-13-2022, 03:29 AM
Last Post: deanhystad
  Why built in functions are defined as class? quazirfan 5 2,717 Oct-23-2021, 01:20 PM
Last Post: Gribouillis
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,883 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 3,529 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Error creating database with python and form? shams 3 2,328 Aug-02-2021, 02:00 PM
Last Post: deanhystad
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,335 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 2,123 Dec-14-2020, 05:45 PM
Last Post: karabakh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020