Menu
CoddyTech

Recap - Inventory Dictionary

Part of the Fundamentals section of Coddy's GDScript journey. Lesson 82 of 92.

challenge icon

Challenge

Easy

A player starts with the inventory {"potion": 2, "arrow": 10}. The program reads three lines of input:

  1. The name of an item the player picks up
  2. How many of it (a whole number)
  3. The name of an item the player uses

Then, in this order:

  1. Add the picked-up amount to that item. An item that is not in the inventory yet starts at 0.
  2. If the inventory has the used item, take 1 away from its count and print Used, a space and the item name. When its count reaches 0, erase the item. If the inventory does not have it, print You have no, a space and the item name.
  3. Print Items:, a space and the number of different items.
  4. 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: 15

For the inputs gem, 3 and bomb, the output is:

You have no bomb
Items: 3
potion: 2
arrow: 10
gem: 3

Try 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

Practice on your own: Online GDScript compiler