Recap - Inventory Dictionary
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 82 of 92.
Challenge
EasyA player starts with the inventory {"potion": 2, "arrow": 10}. The program reads three lines of input:
- The name of an item the player picks up
- How many of it (a whole number)
- The name of an item the player uses
Then, in this order:
- Add the picked-up amount to that item. An item that is not in the inventory yet starts at
0. - If the inventory has the used item, take
1away from its count and printUsed, a space and the item name. When its count reaches0, erase the item. If the inventory does not have it, printYou have no, a space and the item name. - Print
Items:, a space and the number of different items. - Print each item and its count as
item: count, one per line, in inventory order.
For the inputs arrow, 5 and potion, the output is:
Used potion
Items: 2
potion: 1
arrow: 15For the inputs gem, 3 and bomb, the output is:
You have no bomb
Items: 3
potion: 2
arrow: 10
gem: 3Try it yourself
extends Node
func _ready():
var item := OS.read_string_from_stdin()
var amount := int(OS.read_string_from_stdin())
var used := OS.read_string_from_stdin()
var inventory := {"potion": 2, "arrow": 10}
# Write your code here
pass
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators10Functions
Declare a FunctionArgumentsReturnReturn TypesRecap - Sigma FunctionRecap - Validation FunctionDefault Values5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator9Loops
For LoopWhile LoopBreakContinueRecap - FactorialThe Range FunctionNested LoopRecap - Dynamic Input12Arrays Basics
Declaring an ArrayAccessing Array ElementsModifying ArraysArray MethodsRecap - Product ArrayRecap - Reversed ArrayTyped Arrays15Dictionaries
Creating DictionariesAccessing ValuesModifying DictionariesDictionary MethodsIterating Over DictionariesRecap - Inventory DictionaryPractice on your own: Online GDScript compiler