Name Error rand function

This is the place for queries that don't fit in any of the other categories.

Name Error rand function

Postby venator87 » Sat Mar 09, 2013 9:49 pm

I keep getting this error every time I try to run a rand function within a function in the program: NameError: global name 'rand' is not defined

Choose either Feed or Play to lead yourself to the error.

Code: Select all
# Critter Caretaker
# A virtual pet to care for

import random

class Critter(object):

    """A virtual pet"""
    def __init__(self, name, hunger = 0, boredom = 0):
        self.name = name
        self.hunger = hunger
        self.boredom = boredom

    def __pass_time(self):
        self.hunger += 1
        self.boredom += 1

    @property
    def mood(self):
        unhappiness = self.hunger + self.boredom
        if unhappiness < 5:
            m = "happy"
        elif 5 <= unhappiness <= 10:
            m = "okay"
        elif 11 <= unhappiness <= 15:
            m = "frustrated"
        else:
            m = "mad"
        return m
   
    def talk(self):
        print("I'm", self.name, "and I feel", self.mood, "now.\n")
        self.__pass_time()
   
    def eat(self, food = 4):
        food = int(input("How much do you want to feed?: "))
        while food > 5:
            food = int(input("That's too much! How much do you want to feed?: "))
        print("Brruppp.  Thank you.")
        self.hunger -= food
        self.boredom += (food * rand(1, 2.5))
        if self.hunger < 0:
            self.hunger = 0
        self.__pass_time()

    def play(self, fun = 4):
        fun = int(input("How long do you want to play?: "))
        while fun > 5:
            fun = int(input("That's too long! How long do you want to play?: "))
        print("Wheee!")
        self.boredom -= fun
        self.hunger += (fun * rand(1, 2.5))
        if self.boredom < 0:
            self.boredom = 0
        self.__pass_time()


def main():
    crit_name = input("What do you want to name your critter?: ")
    crit = Critter(crit_name)

    choice = None 
    while choice != "0":
        print \
        ("""
        Critter Caretaker
   
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - Play with your critter
        """)
   
        choice = input("Choice: ")
        print()

        # exit
        if choice == "0":
            print("Good-bye.")

        # listen to your critter
        elif choice == "1":
            crit.talk()
       
        # feed your critter
        elif choice == "2":
            crit.eat()
         
        # play with your critter
        elif choice == "3":
            crit.play()

        # some unknown choice
        else:
            print("\nSorry, but", choice, "isn't a valid choice.")

main()
("\n\nPress the enter key to exit.")
venator87
 
Posts: 5
Joined: Sat Mar 09, 2013 9:43 pm

Re: Name Error rand function

Postby micseydel » Sat Mar 09, 2013 10:04 pm

You should have read this before posting, in particular the third bullet of the section titled "What to include in a post".

In answer to your question, when you import a module and you want to access something inside the module, you must prepend the module name with a dot before the function you're using. So you'd use random.rand() instead of just rand(). Or, you can use from random import rand. Any decent tutorial should discuss this.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1118
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Coding Help

Who is online

Users browsing this forum: micseydel and 4 guests