user6471770
user6471770

Reputation:

DiceGame returns an error saying there is no variable when I have declared one?

I'm an amateur 15 year old programmer who's relatively new to functions in Python and have never done OOP Python, however I've delved into it slightly. Our class project is to make a simple dice game however I've tried to expand on my skills. The source code will be below and I will leave comments where I would like help (so don't tell me there's a hash, that's there to let you know where I think I've gone wrong). Whenever I run the program, the console comes up with the following error:

Traceback (most recent call last):
  File "python", line 5, in <module>
  File "python", line 52, in DiceGame
  File "python", line 30, in main
NameError: name 'sides' is not defined

Here is the source code:

#Jabir Hussains Source Code - Dice Task
import random

x = "Error!"

class DiceGame():
  def sides():
    sides = int(input("How many sides would you like on your di[c]e?"))
    if sides > 10:
      print("Sorry, the developer didn't have the time to program above 10 dice")
      sides = int(input("How many sides do you like on your di[c]e?"))
    elif sides < 6:
      print("The limit is 6 sides. Please try again")
      sides = int(input("How many sides do you like on your di[c]e?")) #Here I've defined the number of sides
    else:
      return sides
  sides()

  def diceNum():
    numDice = int(input("How many dice would you like?"))
    if numDice > 2:
      print("We're limited to 2 dice, please try again")
    elif numDice < 1:
      print("That is mathematically impossible. Please try again")
      numDice = int(input("How many dice would you like?"))
    else:
      return numDice
  diceNum()

  def main():
    if sides == 6 and numDice == 1:
      print("The number is",random.randint(1,6),"!")
    elif sides == 6 and numDice == 2:
      print("The numbers are",random.randint(1,6),"and",random.randint(1,6),"!")
    elif sides == 7 and numDice == 1:
      print("The number is",random.randint(1,7),"!")
    elif sides == 7 and numDice == 2:
      print("The numbers are",random.randint(1,7),"and",random.randint(1,7),"!")
    elif sides == 8 and numDice == 1:
      print("The number is",random.randint(1,8),"!")
    elif sides == 8 and numDice == 2:
      print("The numbers are",random.randint(1,8),"and",random.randint(1,8),"!")
    elif sides == 9 and numDice == 1:
      print("The number is",random.randint(1,9),"!")
    elif sides == 9 and numDice == 2:
      print("The numbers are",random.randint(1,9),"and",random.randint(1,9),"!")
    elif sides == 10 and numDice == 1:
      print("The number is",random.randint(1,10),"!")
    elif sides == 10 and numDice == 2:
      print("The numbers are",random.randint(1,10),"and",random.randint(1,10),"!")
    else:
      print(x)
  main()
DiceGame()

Upvotes: 0

Views: 49

Answers (1)

Mihai Matache
Mihai Matache

Reputation: 11

There is so much wrong here I'm not sure where to start.

But the main points would be:

1) You should define a class from "object"

class YourClass(object):

2) You need an init method (it is sort of like a constructor, look this up)

def __init__(self, arg1, arg2):

3) In order to use an object from a class you have to instantiate it

yourObject = YourClass(arg1, arg2)

After you do that you can do:

yourObject.yourFunction()

I hope this helps, but check the documentation:

https://docs.python.org/2.7/

Upvotes: 1

Related Questions