Skip to main content

Moving Between Rooms in Python

Added the map to my game in case that might be helpful in some way? I've tried my best to figure this out on my own using my textbook and the very helpful, useful answers already supplied here. However, it feels as though every attempt to fix my code only makes it worse. I'm certain my loop is causing the issue, but I can't figure out where I went wrong.

It prints everything correctly and asks for the input but, when I type in the input, it only calls the first loop. I thought it might be an indention problem, but PyCharm throws a fit when I indent the second loop. Thank you in advance.

**Updated "quit" or "QUIT" Thanks again, jarmod! ***Updated with latest code, thanks trincot!

Screenshot of output to show current issue:Input is needed twice before updating room and inventory? I see the formatting issue with the inventory. I think I can fix that... Maybe.

Here is my code:

spaces = {
    "the outdoors": {"UP": "Barn", "RIGHT": "Magistrate's Home", "DOWN": "Candy Store"},
    "Flowerhaus": {"RIGHT", "Barn"},
    "Barn": {"LEFT": "Flowerhaus", "RIGHT": "Random Cabin", "DOWN": "the outdoors"},
    "Magistrate's Home": {"UP": "Random Cabin", "DOWN": "Workshop", "LEFT": "the outdoors"},
    "Workshop": {"UP": "Magistrate's Home", "LEFT": "Candy Store"},
    "Candy Store": {"UP": "the outside", "LEFT": "Tavern", "RIGHT": "Workshop", "DOWN": "Papermill"},
    "Tavern": {"RIGHT": "Candy Store", "DOWN": "Empty House"},
    "Empty House": {"UP": "Tavern", "RIGHT": "Papermill"},
    "Papermill": {"UP": "Candy Store", "LEFT": "Empty House"},
    "Random Cabin": {"DOWN": "Magistrate's Home", "LEFT": "Barn"},
}  # Dictionary for rooms and directions

items = {"Flowerhaus": None, "Tavern": None, "Barn": "Car Key", "Random Cabin": "Neighbor's Key",
         "Magistrate's Home": "Deadbolt Key", "Workshop": "Office Key", "Empty House": "House Key",
         "Papermill": "Mailbox Key", "Candy Store": "Ex's Key"}  # Rooms as keys and key items as values.

current_space = "the outdoors"  # Player starts outside
current_inventory = "a whole lot of nothing"
avail_directions = "UP", "DOWN", "RIGHT"

print("It's been a long, long, frustrating day.")  # Print the story
print("You've entered the front door to your home, ready to take your shoes off and relax...")
print("As you walk through the door, you realize this is not your house, your city, or even your realm of existence.")
print("Warily, you step forward to further take in your surroundings.")
print("The floor is dirt, the buildings around you look strange, and the people don't seem to react to your presence.")
print("")
print("After a short time, you realize your keys are no longer in your hand.")
print("As you turn back around, you notice the door is still there, but now it has seven locks, each appearing")
print("to match the locks that go to all seven of your keys.")
print("Turning the knob proves the portal home is locked.")
print("Find all seven keys to unlock the door. Fail to find all seven keys and the door will disappear,")
print("leaving you trapped forever.")
print("")
print("So, with all of that in mind, where would you like to look first?")  # End of printed story
direction = input("Type UP, DOWN, LEFT, or RIGHT").upper()

while direction != exit:
    while direction in avail_directions:
        print("You are currently in", current_space, "with", current_inventory, "on your keyring.")  # Current status
        direction = input("Type UP, DOWN, LEFT, or RIGHT").upper()
        if direction in avail_directions:
            current_space = spaces[current_space][direction]
            avail_directions = tuple(spaces[current_space])
            if current_space in items.keys():
                if current_inventory == "a whole lot of nothing":
                    current_inventory = items[current_space]
                    print("You walk through the door and enter", current_space, end=".")
                    print("You found the", current_inventory, ".".strip())
                else:
                    current_inventory += items[current_space]
                    print("You walk through the door and enter", current_space, end=".")
                    print("You found the", current_inventory, end=".")
            elif direction == "QUIT":
                print("Oh... So, you're going to take a break then? Yeah, maybe a nap is a good idea. Try again later.")
                break
            else:
                print("Nothing over here. Don't you want to get back home? Try a different direction.")
                continue
    else:
        print("Nothing over here. Don't you want to get back home? Try a different direction.")
        direction = input("Type UP, DOWN, LEFT, or RIGHT").upper()


source https://stackoverflow.com/questions/72586007/moving-between-rooms-in-python

Comments

Popular posts from this blog

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype