Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Dungeon Game Win or Lose

My version of the dunegon game with some questions

Hi, I improved and did some of the extra credits, I have few more questions about some parts of the code, and i will be happy to get a review :D

this is the snapshot: https://w.trhou.se/tlc7na9yh2

What i improved?: 1.The monster is moving to a different location if the number of steps is equal to 3(it's 3 for debug purposses, the new location of the monster also printed for debuging purposses.

2.I added the score system which counts how many wins and loses the player had.

However i really struggled at this one but in the end i used the global keyword(i saw an example on stackoverflow) to import the variables to the loop scope, i dont like this way at all.

I tried to accomplish this by setting the count_wins and count_loses variable outside of the while True to prevent the score to rest evreytime the game starts, however i't didnt work and it still rested, this is the code i tried before using global:

    monster, door,player = get_location()
    playing = True
    move_steps = 0
    total_steps = 0
    count_wins = 0
    count_loses = 0
    while playing:
        clear_screen()
        draw_map(player, monster, door)
        valid_moves = get_moves(player)
        print("You're currently in room  {}".format(player))
        #we use join() function to convert the available moves list into a sentence.
        print("You can move {}".format(", ".join(valid_moves)))                
        print("Enter QUIT to quit")
        move = input("> ")
        move = move.upper()
        if move == "QUIT" or move == "Q":
            print("\n *** Thanks for playing, See you next time :D! *** \n")
            break
        if move in valid_moves:
            player = move_player(player,move)
            move_steps += 1
            total_steps += 1
            #for debuging purposes its 3 
            if move_steps == 3:
                print("The monster will be moving from {}".format(monster))
                monster = moving_monster()
                print("The monster has been moved to {}".format(monster))
                input("\n ** Enter to continue the game, be careful, the monster has moved :0 ** \n")
                clear_screen()
            if player == monster:
                clear_screen()
                draw_map(player,monster, door)
                count_loses += 1
                print("\n ** You ran into a hungry monster! It ate you at ({}) :[, You have lost {} games so far **\n".format(monster,count_loses))
                playing = False 
            if player == door:
                clear_screen()
                draw_map(player,monster,door)
                count_wins += 1
                print("\n *** Congratulations :D!,You have found the door, You Won!, It took you {} steps to find the door, You won {} games so far  :P *** \n".format(total_steps, count_wins))
                playing = False


        else:
            input("\n ** {} is not a valid move, Try again :] **\n".format(move))
    else:
        player_decision = input("Do you want to play the awesome game again? Press Y/N: ")
        if player_decision.upper() != "N":
            game_loop()

3.I added the feature that causes the monster to appear if the player step on it, same for the door.

4.each time the user win he gets how many steps took him to get to the door.

Jennifer Nordell Jeff Muday Steven Parker Tabatha Trahan

I will appreciate you're help :]

9 Answers

Steven Parker
Steven Parker
229,744 Points

Since you only have square sizes, it makes sense to ask just one question for size. Then, you don't need an exception, you can simply test that the answer is a valid size and keep looping until it is:

        map_size = 0
        while map_size not in [5, 10]:
            map_size = int(input("Please choose the size of the map [10/5]: "))
        width_of_map = map_size
        height_of_map = map_size
Steven Parker
Steven Parker
229,744 Points

The message "TabError: inconsistent use of tabs and spaces in indentation" means that there is a mix of tabs and spaces in the file. Python can work with either, but it does not allow you to use both in the same file. The editor also applies red highlighting to show where this is happening. Pick one or the other and be consistent (I recommend spaces).

After fixing the indentation the size chooser seems to work as it should. The keyword "in" is called the membership operator and it is a handy way to tell if a specific value is contained in an iterable.

Item 2 isn't really a problem. The use of "global" is normal and correct for this situation. Otherwise, Python considers the total variables to be local and being accessed before they are assigned.

Steven Parker
Steven Parker
229,744 Points

The latest snapshot has these issues:

  • the "else" on line 228 needs four more spaces indent to line up with "if move in valid_moves:" on line 186
  • the "input" on line 229 needs more indent to go with the "else"
  • the "input" on line 229 is missing a closing parenthesis at the end (after the last quote)
  • the "else" on line 230 needs one more space indent to line up with "while playing:" on line 173

And while not immediately fatal, the file still has a mix of tab and space indenting.

UPDATE

I added the option to play at 1 more board size, u can play at 10*10 size or 5*5, It's working if i put the exact input I couldn't figure out how to handle an excpetion if for example i enter 8 and not 5 or 10.

CURRENT SNAPSHOT , THE VERSION IS IN THE trying.py file!!: https://w.trhou.se/502liy615l

I will appreciate if anyone can help me and answer my question and help me with dealing with the exception

And i will appreciate if u can review the code

Hi, first thanks for ur help. However i tried to do what u suggest and im getting a indetneion error even the Identation is fine, How ur able to check if its in [5,10]? i didnt know that u can check if a value is in an array like that. this is the current snapshot:

https://w.trhou.se/jwy6o86qd6

Secondly, you forgot about question 2, with the problem in the scoring system that i had to use the global keyword. this is the snapshot for this step: https://w.trhou.se/tlc7na9yh2

I will appreciate ur help :]

Steven Parker

Steven Parker I dont see any red highlights.. this is the current snapshot: https://w.trhou.se/jtcsqrlvha

Steven Parker
Steven Parker
229,744 Points

In the other snapshot, the editor spacing was set to 4. But highlighted or not, the issue is a mix of spaces and tabs in the indents.

Hey steven, thanks for the help, I tried to fix it, and im stuck on line 230 no matter what i do i get a invalid syntax error. this is the latest snapshot: https://w.trhou.se/8wxzcbqxqu

thanks in advacne!:D

Steven Parker

Steven Parker
Steven Parker
229,744 Points

I added another comment to my answer.

Thanks steven! how i miss this :0, What do u think about my upgrades? and [5,10] is a list right?

I finished python collections and did all the extra credits, however I would like to ask an advise from u should i continue to oop python or try to build more stuff like a RPS GAME, HANGMAN ETC..?

Steven Parker
Steven Parker
229,744 Points

If you want a break from lessons and practice building other projects, great. But as you continue you'll learn other techniques to make your code more concise and featureful. Like that membership operator ("in") to test if something is in an iterable (and yes, in this case it was a list).

And if you think that was cool .. just wait until you see comprehensions and generators. :wink: