Fantasy game inventory — Ch. 5 Automate the Boring StuffDynamic class instancing (with conditional...
Do my partner and son need an SSN to be dependents on my taxes?
How to use random to choose colors
Does cooling a potato change the nature of its carbohydrates?
Would a 7805 5v regulator drain a 9v battery?
Simplify, equivalent for (p ∨ ¬q) ∧ (¬p ∨ ¬q)
How much steel armor can you wear and still be able to swim?
Are there any individual aliens that have gained superpowers in the Marvel universe?
Fantasy game inventory — Ch. 5 Automate the Boring Stuff
Who was the youngest Executive Producer?
Right indicator flash-frequency has increased and rear-right bulb is out
A medieval book with a redhead girl as a main character who allies with vampires and werewolves against scientific opposition
I'm yearning in grey
Checking if argument is a floating point without breaking on control sequences in argument
How can I ping multiple IP addresses at the same time?
How is linear momentum conserved in circular motion?
Can you place a web spell on a surface you cannot see?
How "fast" do astronomical events occur?
What are the mechanical differences between Adapt and Monstrosity?
In a Fish that is not a Fish
Should I email my professor to clear up a (possibly very irrelevant) awkward misunderstanding?
You may find me... puzzling
Fill the maze with a wall-following Snake until it gets stuck
Got a new frameset, don't know why I need this split ring collar?
What is "dot" sign in •NO?
Fantasy game inventory — Ch. 5 Automate the Boring Stuff
Dynamic class instancing (with conditional parameters and methods) based on a dictionaryMy implementation of item objects in a text adventureText-based fighting simulation in C++Comma Code (project from “Automate the Boring Stuff with Python”)Comma Code - Automate the Boring StuffPython Automate the Boring Stuff Collatz exerciseText-Turn based dueling gameAutomate the Boring Stuff with Python - The Collatz sequence projectComma Code - Ch. 4 Automate the Boring StuffAutomate the Boring Stuff - Collatz Exercise
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
Here is a practice exercise — Fantasy Game Inventory $-$
You are creating a fantasy video game. The data structure to model the
player’s inventory will be a dictionary where the keys are string
values describing the item in the inventory and the value is an
integer value detailing how many of that item the player has. For
example, the dictionary value{'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}means the player has 1 rope, 6 torches,
42 gold coins, and so on.
Write a function named
display_inventory()that would take any
possible “inventory” and display it like the following -
Inventory:
12 arrows
42 gold coins
1 rope
6 torches
1 dagger
Total number of items: 62
Hint - You can use a for loop to loop through all the keys in a
dictionary.
I have written the following code. Any feedback is highly appreciated.
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inventory):
total_items = 0
print ("Inventory:")
for item in inventory:
print(str(inventory[item]) + ' ' + item)
total_items += inventory[item]
print("Total number of items: " + str(total_items))
if __name__ == '__main__':
display_inventory(stuff)
python performance python-3.x formatting role-playing-game
$endgroup$
add a comment |
$begingroup$
Here is a practice exercise — Fantasy Game Inventory $-$
You are creating a fantasy video game. The data structure to model the
player’s inventory will be a dictionary where the keys are string
values describing the item in the inventory and the value is an
integer value detailing how many of that item the player has. For
example, the dictionary value{'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}means the player has 1 rope, 6 torches,
42 gold coins, and so on.
Write a function named
display_inventory()that would take any
possible “inventory” and display it like the following -
Inventory:
12 arrows
42 gold coins
1 rope
6 torches
1 dagger
Total number of items: 62
Hint - You can use a for loop to loop through all the keys in a
dictionary.
I have written the following code. Any feedback is highly appreciated.
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inventory):
total_items = 0
print ("Inventory:")
for item in inventory:
print(str(inventory[item]) + ' ' + item)
total_items += inventory[item]
print("Total number of items: " + str(total_items))
if __name__ == '__main__':
display_inventory(stuff)
python performance python-3.x formatting role-playing-game
$endgroup$
3
$begingroup$
The interesting part of this task is to generate the correct plural forms from the singulars. You completely missed this one.
$endgroup$
– Roland Illig
9 hours ago
add a comment |
$begingroup$
Here is a practice exercise — Fantasy Game Inventory $-$
You are creating a fantasy video game. The data structure to model the
player’s inventory will be a dictionary where the keys are string
values describing the item in the inventory and the value is an
integer value detailing how many of that item the player has. For
example, the dictionary value{'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}means the player has 1 rope, 6 torches,
42 gold coins, and so on.
Write a function named
display_inventory()that would take any
possible “inventory” and display it like the following -
Inventory:
12 arrows
42 gold coins
1 rope
6 torches
1 dagger
Total number of items: 62
Hint - You can use a for loop to loop through all the keys in a
dictionary.
I have written the following code. Any feedback is highly appreciated.
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inventory):
total_items = 0
print ("Inventory:")
for item in inventory:
print(str(inventory[item]) + ' ' + item)
total_items += inventory[item]
print("Total number of items: " + str(total_items))
if __name__ == '__main__':
display_inventory(stuff)
python performance python-3.x formatting role-playing-game
$endgroup$
Here is a practice exercise — Fantasy Game Inventory $-$
You are creating a fantasy video game. The data structure to model the
player’s inventory will be a dictionary where the keys are string
values describing the item in the inventory and the value is an
integer value detailing how many of that item the player has. For
example, the dictionary value{'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}means the player has 1 rope, 6 torches,
42 gold coins, and so on.
Write a function named
display_inventory()that would take any
possible “inventory” and display it like the following -
Inventory:
12 arrows
42 gold coins
1 rope
6 torches
1 dagger
Total number of items: 62
Hint - You can use a for loop to loop through all the keys in a
dictionary.
I have written the following code. Any feedback is highly appreciated.
stuff = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inventory):
total_items = 0
print ("Inventory:")
for item in inventory:
print(str(inventory[item]) + ' ' + item)
total_items += inventory[item]
print("Total number of items: " + str(total_items))
if __name__ == '__main__':
display_inventory(stuff)
python performance python-3.x formatting role-playing-game
python performance python-3.x formatting role-playing-game
edited 5 hours ago
Justin
asked 10 hours ago
JustinJustin
1,640528
1,640528
3
$begingroup$
The interesting part of this task is to generate the correct plural forms from the singulars. You completely missed this one.
$endgroup$
– Roland Illig
9 hours ago
add a comment |
3
$begingroup$
The interesting part of this task is to generate the correct plural forms from the singulars. You completely missed this one.
$endgroup$
– Roland Illig
9 hours ago
3
3
$begingroup$
The interesting part of this task is to generate the correct plural forms from the singulars. You completely missed this one.
$endgroup$
– Roland Illig
9 hours ago
$begingroup$
The interesting part of this task is to generate the correct plural forms from the singulars. You completely missed this one.
$endgroup$
– Roland Illig
9 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
I am suggesting to use fstrings and the dictionary items() method.
The
print(f'{value} {key}')
instead of
print(str(inventory[item]) + ' ' + item)
is more neatly:
def display_inventory(inventory):
total_items = 0
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
total_items += value
print(f'Total number of items: {total_items}')
Also, you can just calculate the total number in the needed place by the sum() function and the dictionary values() method. Then, you are not needing the total_items variable.
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
$endgroup$
add a comment |
$begingroup$
As mentioned in a comment by Roland Illig, I missed the interesting part of generating the correct plural forms from the singulars.
Here's a module which supports Python 3 - Inflect.
# Initialization
import inflect
p = inflect.engine()
Examples -
word = car
print("The plural of ", word, " is ", p.plural(word))
# cars
word = cars
print("The singular of ", word, " is ", p.singular_noun(word))
# car
My updated code, expanding on MiniMax's answer, is:
import inflect
p = inflect.engine()
stuff = {'rope': 0, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
if value == 0 or value > 1:
key = p.plural(key)
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
if __name__ == '__main__':
display_inventory(stuff)
This will give you the following output -
Inventory:
0 ropes
6 torches
42 gold coins
1 dagger
12 arrows
Total number of items: 61
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f222354%2ffantasy-game-inventory-ch-5-automate-the-boring-stuff%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
I am suggesting to use fstrings and the dictionary items() method.
The
print(f'{value} {key}')
instead of
print(str(inventory[item]) + ' ' + item)
is more neatly:
def display_inventory(inventory):
total_items = 0
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
total_items += value
print(f'Total number of items: {total_items}')
Also, you can just calculate the total number in the needed place by the sum() function and the dictionary values() method. Then, you are not needing the total_items variable.
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
$endgroup$
add a comment |
$begingroup$
I am suggesting to use fstrings and the dictionary items() method.
The
print(f'{value} {key}')
instead of
print(str(inventory[item]) + ' ' + item)
is more neatly:
def display_inventory(inventory):
total_items = 0
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
total_items += value
print(f'Total number of items: {total_items}')
Also, you can just calculate the total number in the needed place by the sum() function and the dictionary values() method. Then, you are not needing the total_items variable.
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
$endgroup$
add a comment |
$begingroup$
I am suggesting to use fstrings and the dictionary items() method.
The
print(f'{value} {key}')
instead of
print(str(inventory[item]) + ' ' + item)
is more neatly:
def display_inventory(inventory):
total_items = 0
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
total_items += value
print(f'Total number of items: {total_items}')
Also, you can just calculate the total number in the needed place by the sum() function and the dictionary values() method. Then, you are not needing the total_items variable.
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
$endgroup$
I am suggesting to use fstrings and the dictionary items() method.
The
print(f'{value} {key}')
instead of
print(str(inventory[item]) + ' ' + item)
is more neatly:
def display_inventory(inventory):
total_items = 0
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
total_items += value
print(f'Total number of items: {total_items}')
Also, you can just calculate the total number in the needed place by the sum() function and the dictionary values() method. Then, you are not needing the total_items variable.
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
answered 8 hours ago
MiniMaxMiniMax
22117
22117
add a comment |
add a comment |
$begingroup$
As mentioned in a comment by Roland Illig, I missed the interesting part of generating the correct plural forms from the singulars.
Here's a module which supports Python 3 - Inflect.
# Initialization
import inflect
p = inflect.engine()
Examples -
word = car
print("The plural of ", word, " is ", p.plural(word))
# cars
word = cars
print("The singular of ", word, " is ", p.singular_noun(word))
# car
My updated code, expanding on MiniMax's answer, is:
import inflect
p = inflect.engine()
stuff = {'rope': 0, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
if value == 0 or value > 1:
key = p.plural(key)
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
if __name__ == '__main__':
display_inventory(stuff)
This will give you the following output -
Inventory:
0 ropes
6 torches
42 gold coins
1 dagger
12 arrows
Total number of items: 61
$endgroup$
add a comment |
$begingroup$
As mentioned in a comment by Roland Illig, I missed the interesting part of generating the correct plural forms from the singulars.
Here's a module which supports Python 3 - Inflect.
# Initialization
import inflect
p = inflect.engine()
Examples -
word = car
print("The plural of ", word, " is ", p.plural(word))
# cars
word = cars
print("The singular of ", word, " is ", p.singular_noun(word))
# car
My updated code, expanding on MiniMax's answer, is:
import inflect
p = inflect.engine()
stuff = {'rope': 0, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
if value == 0 or value > 1:
key = p.plural(key)
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
if __name__ == '__main__':
display_inventory(stuff)
This will give you the following output -
Inventory:
0 ropes
6 torches
42 gold coins
1 dagger
12 arrows
Total number of items: 61
$endgroup$
add a comment |
$begingroup$
As mentioned in a comment by Roland Illig, I missed the interesting part of generating the correct plural forms from the singulars.
Here's a module which supports Python 3 - Inflect.
# Initialization
import inflect
p = inflect.engine()
Examples -
word = car
print("The plural of ", word, " is ", p.plural(word))
# cars
word = cars
print("The singular of ", word, " is ", p.singular_noun(word))
# car
My updated code, expanding on MiniMax's answer, is:
import inflect
p = inflect.engine()
stuff = {'rope': 0, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
if value == 0 or value > 1:
key = p.plural(key)
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
if __name__ == '__main__':
display_inventory(stuff)
This will give you the following output -
Inventory:
0 ropes
6 torches
42 gold coins
1 dagger
12 arrows
Total number of items: 61
$endgroup$
As mentioned in a comment by Roland Illig, I missed the interesting part of generating the correct plural forms from the singulars.
Here's a module which supports Python 3 - Inflect.
# Initialization
import inflect
p = inflect.engine()
Examples -
word = car
print("The plural of ", word, " is ", p.plural(word))
# cars
word = cars
print("The singular of ", word, " is ", p.singular_noun(word))
# car
My updated code, expanding on MiniMax's answer, is:
import inflect
p = inflect.engine()
stuff = {'rope': 0, 'torch': 6, 'gold coin': 42, 'dagger': 1, 'arrow': 12}
def display_inventory(inventory):
print ("Inventory:")
for key, value in inventory.items():
if value == 0 or value > 1:
key = p.plural(key)
print(f'{value} {key}')
print(f'Total number of items: {sum(inventory.values())}')
if __name__ == '__main__':
display_inventory(stuff)
This will give you the following output -
Inventory:
0 ropes
6 torches
42 gold coins
1 dagger
12 arrows
Total number of items: 61
edited 3 hours ago
AJNeufeld
8,7201831
8,7201831
answered 6 hours ago
JustinJustin
1,640528
1,640528
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f222354%2ffantasy-game-inventory-ch-5-automate-the-boring-stuff%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
$begingroup$
The interesting part of this task is to generate the correct plural forms from the singulars. You completely missed this one.
$endgroup$
– Roland Illig
9 hours ago