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): = name
        self.hunger = hunger
        self.boredom = boredom

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

    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"
            m = "mad"
        return m
    def talk(self):
        print("I'm",, "and I feel", self.mood, "now.\n")
    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

    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?: "))
        self.boredom -= fun
        self.hunger += (fun * rand(1, 2.5))
        if self.boredom < 0:
            self.boredom = 0

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: ")

        # exit
        if choice == "0":

        # listen to your critter
        elif choice == "1":
        # feed your critter
        elif choice == "2":
        # play with your critter
        elif choice == "3":

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

("\n\nPress the enter key to exit.")
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!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
Posts: 1993
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 7 guests