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 7742181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:13:03+00:00 2026-06-01T09:13:03+00:00

I am working on a little text base resultant game, but I am having

  • 0

I am working on a little text base resultant game, but I am having troubling creating this list of Food, I want to put all my Food (either Burger, or Hotdog) in listOfFood. In my example I only included two, but they are all “base” on food.

Later then I will iterate through my listOfFood to do something. I abstracted my code to ask question (because it has too many lines, and the other information is not related to it).

class Food():
    def __init__(self):
        self.name = 'Food'

class HotDog(Food):
    def __init__(self):
        Food.__init__(self)

class Burger(Food):
    def __init__(self):
        Food.__init__(self)

def createFoodList(myOrders):
    # A list that contain all the food (either Burger or Hotdog)
    listOfFood = []

    # Depend what is the type of the food, create that many food object
    # and then append it to the list of food
    if myOrders[0][0] == 'HotDog':
        for number in myOrder[0][1]:
            listOfFood.append(HotDot())
    if myOrders[0][1] == 'Burger':
        for number in myOrder[0][1]:
            listOfFood.append(Burger())

    return listOfFood


todayOrders = [['HotDog', 10], ['Burger', 5]]
print(createFoodList(todayOrders))

I wonder if I can use list comprehension or similar method to make my createFoodList function better? Because my idea is to have many different kind of Food, so if I can return this list of food, base on todayOrders list ['type of food', count] and return [food, food, food.. ]. The way I am doing right now is really complicated and long (I have a feeling this is not the right way).

Thank you very much.

Edit: Besides the list comprehension, what can I do to replace the if statements in createFoodList? It is decide on the [[food, count], [food, count]..], and I create that food and append it to my list.

  • 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-01T09:13:05+00:00Added an answer on June 1, 2026 at 9:13 am
    foodmap = {'HotDog': HotDog, 'Burger': Burger}
    
    print [foodmap[f]() for (f, c) in todayOrders for x in range(c)]
    

    foodmap[f] evaluates to either HotDog or Burger (these are classes – not class names, the classes themselves). Calling someclass() creates an instance of soemclass and passes no arguments when calling someclass.__init__. Hence foodmap[f]() combines both of these – it looks up the class in foodmap and uses the class to create an instance of that class.

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

Sidebar

Related Questions

I am working on a little text based console game using Python. Because I
I'm currently working on a little text-based game after watching the new Tron movie
I have a little text based game i am working on. The user has
My python script is working now, but I'm having a little trouble: Here is
I've started working a little bit with lift+scala+mongorecord but I found a small annoyance
I am working on a little card-swapping world-travel game that I sort of envision
I'm working on a Text Addition feature for my App. This text is going
I have little quiz game I've working on and am tracking correct answers. I
I am working on this small little piece in python and when I run
I've been working on making my own little text editor using a RichTextBox(MyRTB). I've

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.