Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:59:29+00:00 2026-06-18T02:59:29+00:00

Ok, so I have this class that’s supposed to have food , hunger ,

  • 0

Ok, so I have this class that’s supposed to have food, hunger, and some other attributes.

I have just a definition like this:

def setfood():
     choice = input("Choice: ")
     if choice == "1":
           food = 1
           bored = 2
     if choice == "2":
           food = 4
           bored = 8
     return food
     return bored

And then I have this class like this.

class thing(object):
    def __init__(self, name, hunger = 0, boredom = 0):
        self.name = name
        self.hunger = hunger
        self.boredom = boredom

    def eat(self):
       setfood()
       self.hunger -= food
       self.boredom += bored

Well, no matter what I do, I can’t get it to work. I kept getting this error about a global variable.

Well, I add in food = 0 and bored = 0 into the main part of the program and it makes no difference because I can’t pass the global variables into the definition.

I tried to add the definition into my class and it still made no difference. I also tried to make separate definitions to return each variables…also didn’t work. No matter what I do, I can’t win.

The only way I can get it to work is if I do this:

food = setfood()

I thought I could maybe change it to this:

food = setfood(food)
food = setfood(bored)

But that doesn’t work either…

But, if I do that for both variables, I have to go through the whole thing twice. So what exactly do I do? I’m so lost and frustrated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:59:30+00:00Added an answer on June 18, 2026 at 2:59 am

    “NameError: global name ‘food’ is not defined”

    food in your function is only defined when you enter one of the if blocks. Are you entering input other than 1 or 2? If os food won’t be defined to anything. You could have a default value for food like:

    food = 0 # food stays at 0 for invalid choices
    choice = input("Choice: ")
    if choice == "1":
         food = 1
         bored = 2
    if choice == "2":
         food = 4
         bored = 8
    return food
    

    Also to return two variables your return statements need to return a tuple, ie

     return (food, bored)
    

    then the caller can do

     (food, bored) = setfood()
    

    and you’ll be able to return two values at once as you intend

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have this class that starts like this: class emailreader(): def __init__(self, server,
I have this Person class that is part of various other transactions like a
I have this class that have a function to load other classes and create
I have class that looks like this: class A { public: class variables_map vm
I have a class that contains this class method: def self.get_event_record(row, participant) event =
I have this small class called City that simply holds some information about a
I have a Handler class that looks like this: class Handler{ public $group; public
Assume I have a class that looks like this: class Sample { public string
Write now I have this class that I would like to be able to
So i have this class that includes some custom functions named class.php I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.