Question

In: Computer Science

PYTHON Exercise 4. Fantasy Game Inventory Imagine you have this inventory in a fantasy game: Item...

PYTHON Exercise 4. Fantasy Game Inventory Imagine you have this inventory in a fantasy game: Item Number of this items Rope 1 Torch 6 Gold coin 42 Dagger 1 Arrow 12 1. Save the inventory in a data structure. 2. Write a function that will take any possible inventory and display it in a pretty format. 3. Write a function to add new items to the data structure. Make sure new group of items can be created. 4. Write a function to delete items (not a whole group) from the data structure.

Solutions

Expert Solution

# 1. Save the inventory in a data structure
inventory = {"Rope": 1, "Torch": 6, "Gold": 42, "Dagger": 1, "Arrow": 12}

# 2. Write a function that will take any possible inventory and display it in a pretty format.
def printInventory(d):
    print("Item\t\tCount")
    print("-----------------")
    for x in d.keys():
        print(str(x)+"\t\t"+str(d[x]))
    print("\n")

# 3. Write a function to add new items to the data structure. Make sure new group of items can be created.
def addInventory(d, item, count):
    d[item] = count

# 4. Write a function to delete items (not a whole group) from the data structure.
def deleteInventory(d, item):
    del d[item]

# Testing methods
printInventory(inventory)
addInventory(inventory,"Silver",23)
printInventory(inventory)
deleteInventory(inventory,"Gold")
printInventory(inventory)


Related Solutions

Imagine that you own a sandwich shop and you have 2 employees. For this exercise, develop...
Imagine that you own a sandwich shop and you have 2 employees. For this exercise, develop the operating portion of a master budget. You need to create seven independent budgets and a budgeted income statement: No. 1 sales budget No. 2 production budget, No. 3 direct materials budget No. 4 direct labor budget No. 5 manufacturing overhead budget No. 6 selling No. 7 administrative expense budget No. 8 budgeted income statement
In this python program , you are to build a trivia game. The game should present...
In this python program , you are to build a trivia game. The game should present each question – either in order or randomly – to the player, and display up to four possible answers. The player is to input what they believe to be the correct answer.   The game will tell the player if they got it right or wrong and will display their score. If they got it right, their score will go up. If they got it...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
exercise 5.2 Develop Your Own Commercial Imagine that you are your own “product.” You have to...
exercise 5.2 Develop Your Own Commercial Imagine that you are your own “product.” You have to sell yourself and your potential contributions to a potential employer or buyer. What would you sell? For example, what are some of the strengths you bring to the table? (Hint: Review the section in this chapter on seeing yourself as a “brand.”) On a separate sheet of paper, spend 5–10 minutes free writing and brainstorming about you as a commercial. Next, what action or...
Imagine you are on a the game show "Let's Make a Deal" and are given the...
Imagine you are on a the game show "Let's Make a Deal" and are given the opportunity to select one closed door of three, behind one of which there is a prize. The other two doors hide goats or some other such non-prize, or nothing at all. Once you have made your selection, Monty Hall will open one of the remaining doors, revealing that it does not contain the prize. He then asks you if you would like to switch...
An Exercise for Your Imagination Imagine you have been following a path for what seems time...
An Exercise for Your Imagination Imagine you have been following a path for what seems time without measure during a morning of swirling mists and diffused light when you feel the ground give softly under your leading foot. As your momentum carries you forward, wet ribbon-like strands hit your face before rubbing along either side, some few catching under your arms and between the fingers of your hands. Surprised, you stop and try to orient yourself, failing until a moment...
Imagine you have some workers and some handheld computers that you can use to take inventory...
Imagine you have some workers and some handheld computers that you can use to take inventory at a warehouse. There are diminishing returns to taking inventory. If one worker uses one computer, he can inventory 150 items per hour. Two workers sharing a computer can together inventory 200 items per hour. Three workers sharing a computer can together inventory 220 items per hour. And four or more workers sharing a computer can together inventory fewer than 235 items per hour....
Imagine you have some workers and some handheld computers that you can use to take inventory...
Imagine you have some workers and some handheld computers that you can use to take inventory at a warehouse. There are diminishing returns to taking inventory. If one worker uses one computer, he can inventory 150 items per hour. Two workers sharing a computer can together inventory 200 items per hour. Three workers sharing a computer can together inventory 250 items per hour. And four or more workers sharing a computer can together inventory fewer than 260 items per hour....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT