🤖

Nimbrights

Python Programming Tutorial - text-based game

Python Dungeon Crawler - Basic Data Types

We will focus on learning the fundamental data types through something fun and engaging rather than something boring found in regular tutorials. This way you will encounter novelty which will help you recall the information later. Our brains thrive on novelty and surprise which leads to stronger memories and easier recall.

It's best if you spend time learning Python everyday, even if it's only 15 or 20 minutes. The constant exposure will make it easier for you to recall all that you learned. Don't worry if you forget something along the way; look it up or keep a work journal.

When you read code, push through the uncertainty you feel. Exposure to code will help you learn to read it, even if you don't understand everything that is going on. Trust me, this is a process.

1. Integers (int)

The Concept: Integers are whole numbers. Perfect for counting items in your bag.

Scenario: You find a torch on the dungeon floor. You need to pick it up and update your count.

# Initializing the variable
torches = 3

print(f"You see a torch on the floor. You currently have {torches}.")

# Updating the variable (Integer addition)
torches = torches + 1

print(f"Inventory Updated: You now carry {torches} torches.")
The f in Python refers to f-strings (formatted string literals). They provide a concise way to embed expressions directly within string literals using {}. All input is assumed to be a string, but you can force an integer by enclosing it in braces.

2. Floats (float)

The Concept: Floats represent numbers with decimal points. Use these for precise stats like health or weight.

Scenario: A poison trap grazes your arm, reducing your health by a fractional amount.

health = 100.0
damage = 15.5

print(f"Current Health: {health}%")

# Updating a float variable
health = health - damage

print(f"You took damage! Health is now: {health}%")

3. Strings (str)

The Concept: Strings are text. In Python, we create them by wrapping text in quotes.

Scenario: A guard blocks your path and demands to know who you are.

# Prompting the user for input
player_name = input("The guard growls: 'State your name!' ")

# Combining strings (Concatenation)
message = "The hero " + player_name + " has entered the Great Hall."

print(message)

4. Lists (list)

The Concept: A list is an ordered collection of items. It is "mutable," meaning you can add or remove things.

Scenario: You open a chest and find a 'Magic Map'. You must add it to your inventory.

inventory = ["Dagger", "Bread", "Key"]

# Adding to the list
inventory.append("Magic Map")

print(f"You open the chest. Your inventory: {inventory}")

5. Tuples (tuple)

The Concept: Tuples are immutable collections. Use them for fixed data like map coordinates.

Scenario: Your position on the X and Y axis of the dungeon.

# Coordinates that shouldn't be changed accidentally
spawn_point = (0, 12)

print(f"If you die, you will restart at: {spawn_point}")

6. Dictionaries (dict)

The Concept: Dictionaries store "Key: Value" pairs. Great for detailed item descriptions.

Scenario: You inspect a sword to see its name and damage power.

weapon = {
    "name": "Sunblade",
    "power": 25
}

print(f"Inspecting {weapon['name']}... it has {weapon['power']} attack power.")

# Updating a dictionary value
weapon["power"] = 30
print("You sharpened the blade! Power is now 30.")