Assignment Chef icon Assignment Chef
All English tutorials

Programming lesson

Python Conditionals in Game Development: Building a Mushroom Trading System

Learn how to use variables, boolean logic, and conditionals in Python by building a mushroom trading system for a text adventure game. This tutorial covers input handling, nested if statements, and exchange logic with rubies.

Python conditionals boolean logic Python variables in Python game development Python mushroom trading game CSCI 141 assignment Python if elif else nested conditionals Python text adventure game logic Python input handling ruby exchange formula programming tutorial 2026 Python beginner project conditional statements examples game logic programming Python assignment help

Introduction: Bringing Logic to a Fungus-Filled Forest

Imagine you're a developer at a game studio called Fungi, crafting a text adventure where a character wanders through a forest collecting mushrooms. In this scenario, the player encounters a chef willing to trade rubies for mushrooms. This assignment from CSCI 141 Spring 2019 teaches you to implement the chef's secret exchange formula using Python. By the end of this tutorial, you'll understand how to use variables, boolean logic, and conditionals to create interactive, decision-based systems.

This isn't just a coding exercise—it's a microcosm of real-world game logic. From inventory management to NPC interactions, conditionals drive every rule-based system. Let's break down the problem step by step.

Understanding the Core Concepts

Variables: Storing Player Data

In Python, variables are containers for data. For this game, you'll need to track the number of shiitake and portobello mushrooms the player has collected. For example:

shiitake_found = int(input("How many shiitake mushrooms did you find? "))
portobello_found = int(input("How many portobello mushrooms did you find? "))

These variables store integers that represent the player's inventory. Later, you'll also store the number of mushrooms the player wants to trade.

Boolean Logic: Making Decisions

Boolean logic evaluates conditions as True or False. For instance, checking if the player tries to trade more mushrooms than they have:

if trade_shiitake > shiitake_found or trade_portobello > portobello_found:
    print("Chef ends the conversation.")

This uses the or operator to combine two conditions. If either is true, the chef refuses the trade.

Conditionals: The if-elif-else Structure

Python's if, elif, and else let you execute different code blocks based on conditions. The assignment restricts you to 10 if keywords, so plan your logic carefully. For example, the chef's exchange rules use multiple conditions:

if condition1:
    rubies = ...
elif condition2:
    rubies = ...

Each rule is a separate branch.

Designing the Mushroom Exchange Logic

The chef's secret formula depends on the quantities of shiitake and portobello the player is willing to trade. Let's translate the pseudocode into Python conditions.

Rule 1: Fewer than 10 shiitake AND fewer than 5 portobello

If the player offers fewer than 10 shiitake and fewer than 5 portobello, the chef gives twice the number of shiitake offered in rubies.

if trade_shiitake < 10 and trade_portobello < 5:
    rubies = 2 * trade_shiitake

Rule 2: Fewer than 10 shiitake AND 5 or more portobello

If shiitake are still under 10 but portobello are 5 or more, rubies equal three times the portobello offered.

elif trade_shiitake < 10 and trade_portobello >= 5:
    rubies = 3 * trade_portobello

Rule 3: Shiitake multiple of 12 but not multiple of 24 AND portobello 20 or more

This condition checks if trade_shiitake % 12 == 0 and trade_shiitake % 24 != 0. If true and portobello are at least 20, rubies are four times the portobello.

elif trade_shiitake % 12 == 0 and trade_shiitake % 24 != 0 and trade_portobello >= 20:
    rubies = 4 * trade_portobello

Rule 4: Shiitake multiple of 12 but not multiple of 24 AND portobello fewer than 20

Same shiitake condition, but portobello less than 20 gives rubies equal to the number of portobello.

elif trade_shiitake % 12 == 0 and trade_shiitake % 24 != 0 and trade_portobello < 20:
    rubies = trade_portobello

Rule 5: Any other shiitake count (the default)

If none of the above match, rubies are five times the shiitake offered.

else:
    rubies = 5 * trade_shiitake

This covers all cases. Note that the order matters: Python checks conditions from top to bottom, so the first true condition executes.

Handling Edge Cases

The assignment specifies three scenarios that end the conversation without a trade:

  • Trade more than collected: If either mushroom type exceeds what the player has, the chef walks away.
  • Zero total mushrooms: If the sum of both mushrooms to trade is zero, the chef is unwilling.
  • Player declines: After the chef offers rubies, the player can say no.

These require early checks. For example:

if trade_shiitake > shiitake_found or trade_portobello > portobello_found:
    print("Chef runs away!")
elif trade_shiitake + trade_portobello == 0:
    print("Unwilling to trade.")

If the trade proceeds, calculate rubies, then ask for confirmation. Use if answer in ['y', 'yes', 'Yes']: to accept.

Putting It All Together: A Complete Program Structure

Here's a skeleton of the program. Note the use of comments and meaningful variable names.

# Author: Your Name
# Date: May 31, 2026
# Description: Mushroom trading system for Fungi game

# Input: mushrooms found
shiitake_found = int(input("How many shiitake mushrooms did you find? "))
portobello_found = int(input("How many portobello mushrooms did you find? "))

# Narrative
print("You meander through the forest...")

# Input: mushrooms to trade
trade_shiitake = int(input("How many shiitake to trade? "))
trade_portobello = int(input("How many portobello to trade? "))

# Edge cases
if trade_shiitake > shiitake_found or trade_portobello > portobello_found:
    print("Chef runs away!")
elif trade_shiitake + trade_portobello == 0:
    print("Unwilling to trade.")
else:
    # Calculate rubies based on rules
    if trade_shiitake < 10 and trade_portobello < 5:
        rubies = 2 * trade_shiitake
    elif trade_shiitake < 10 and trade_portobello >= 5:
        rubies = 3 * trade_portobello
    elif trade_shiitake % 12 == 0 and trade_shiitake % 24 != 0 and trade_portobello >= 20:
        rubies = 4 * trade_portobello
    elif trade_shiitake % 12 == 0 and trade_shiitake % 24 != 0 and trade_portobello < 20:
        rubies = trade_portobello
    else:
        rubies = 5 * trade_shiitake
    
    # Ask player to accept
    answer = input("Do you want to make the exchange? ")
    if answer in ['y', 'yes', 'Yes']:
        remaining_shiitake = shiitake_found - trade_shiitake
        remaining_portobello = portobello_found - trade_portobello
        print(f"You walk away with {remaining_shiitake} shiitake, {remaining_portobello} portobello, and {rubies} rubies.")
    else:
        print(f"You walk away with {shiitake_found} shiitake and {portobello_found} portobello.")

This uses exactly 10 if keywords (including the elif and else). Count them: the first if for edge case, elif for zero, else for main block; inside: if for rule 1, elif for rule 2, elif for rule 3, elif for rule 4, else for default; then if for acceptance check, else for decline. That's 10.

Testing Your Program

Use the sample test cases from the assignment to verify correctness. For example:

  • Input: shiitake found=10, portobello found=30, trade shiitake=5, trade portobello=22 → Chef offers 66 rubies (rule 3: shiitake not multiple of 12? Actually 5 is not, so default rule 5 gives 5*5=25? Wait, check table: 10/5 and 30/22 gives 66 rubies. That corresponds to rule 3? Let's see: 5 shiitake is not multiple of 12, so rule 5 applies: 5*5=25, but expected 66. There's a mistake. Actually the table says Shiitakes Found / Willing to Trade: 10/5, Portobellos Found / Willing to Trade: 30/22, Chef Offers: 66 rubies. That doesn't match our rules. Let's re-examine the pseudocode.

Wait, the assignment's exchange rules are:

  • Fewer than 10 shiitake and fewer than 5 portobello: Twice shiitake
  • Fewer than 10 shiitake and 5 or more portobello: Three times portobello
  • Multiple of 12 but NOT multiple of 24 shiitake and 20 or more portobello: Four times portobello
  • Multiple of 12 but NOT multiple of 24 shiitake and fewer than 20 portobello: Portobello count
  • Any other shiitake: Five times shiitake

For 5 shiitake and 22 portobello: shiitake < 10, portobello >=5 → rule 2: 3 * 22 = 66. Yes! That's correct. So rule 2 applies. Good.

Test other cases from the table to ensure your logic works.

Trend Connection: AI and Game Logic

Just as AI chatbots use conditionals to respond to user input, your mushroom trading system uses boolean logic to simulate a chef's decision-making. In 2026, AI-driven NPCs in games like Cyberpunk 2077 or The Last of Us Part III use similar conditional trees to create realistic interactions. Understanding conditionals is the first step toward building intelligent systems.

Conclusion

You've learned to implement a trading system using Python variables, boolean logic, and conditionals. This foundational skill applies to any rule-based programming, from game development to financial algorithms. Keep experimenting with different inputs and edge cases to solidify your understanding.

For extra credit, try the challenge problem: write a program to find the median and mean of three integers without using libraries. It's a great exercise in conditionals and arithmetic.