Jump to content
Stef's Coding Community

cant see where I've gone wrong?


Recommended Posts

Hi 

I'm currently working my way through Stef's Python course (which is great btw) 

I'm upto finalising the treasure hunt game code, but when I run it, it's not asking me if I'd like to play again it's just going straight to which cave would you like to choose.

I've been following along and typing all my own code as opposed to just copying and pasting from the source file.

I've even pulled up the source file and just can't see what I've done wrong or differently.

This has been bugging me for a few days now, can anyone advise please?

 

here's my copy of the code:

 

# Python treasure hunt game


import random, time

def display_game_intro():
    print('''

           ---->Welcome to the 'Python Treasure Hunt Game'
           ...the most amazing game ever made!

           After a long journey, you find yourself in front of two caves.
           One cave leads to a treasure, the other, a spike filled pit!
           Being brave, and a little greedy for treasure, you've decided
           to go for it!
       ''')


def choose_cave():
    cave = ''
    while cave != '1' and cave != '2':
        print('Which cave do you want to enter? (1 or 2)')
        cave = input()
    return cave


def enter_cave(chosen_cave):
    print('\nYou have entered a cave...')
    time.sleep(1)

    random_cave = random.randint(1,2)
    #print("random_cave= " + str(random_cave))

    if random_cave == int(chosen_cave):
        print("---> Lucky you! You found the chest!")
    else:
            print("--->You expected to find a chest\n ...and all you found was DEATH!")

            
    


def main_loop():
    ''' The main_loop() function controls the flow of the game by
calling functions and using conditionals'''

    playGameAgain = "yes"

    while playGameAgain == "yes" or playGameAgain == "y":
        
            display_game_intro()
            chosen_cave = choose_cave()
            enter_cave(chosen_cave)

    print("\n\nDo you want to try again? (yes or no)")
    playGameAgain = input()
    print("You said: " + playGameAgain)
    time.sleep(1)

    if playGameAgain == "yes" or playGameAgain == "y":
        print("\nLet's try again!")

    else:
        print("\nOk, see you later!")

 

main_loop()

Link to post
Share on other sites

Just for the record (In case anybody would like to check my typed code vs the source code, I will include it here:

 

# Python Treasure Hunt Game
# Finding a simple data type bug. 

import random, time

def display_game_intro():
    print('''
             ----> Welcome to the 'Python Treasure Hunt Game'
             ... the most amazing game ever made!

             After a long journey, you find yourself in front of two caves.
             One cave leads to a treasure, the other, a spike filled pit!
             Being brave, and a little greedy for treasure, you've
             decided to go for it!
             ''')

def choose_cave():
    cave = ''
    while cave != '1' and cave != '2':
        print('Which cave do you want to enter? (1 or 2)')
        cave = input()
    return cave


def enter_cave(choosen_cave):
    print('\nYou have entered a cave ...')
    time.sleep(1)

    random_cave = random.randint(1,2)
    #print("randon_cave= " + str(random_cave))
    
    if random_cave == int(choosen_cave):
        print("---> Lucky you! You found the chest!")
    else:
        print("---> You expected to find a chest\n ...and all you found was DEATH!")
      
        

def main_loop():
    
    ''' The main_loop() function controls the flow of the game by
calling functions and using conditionals'''
    
    playGameAgain = "yes"
    
    while playGameAgain == "yes" or playGameAgain == "y":
        
            display_game_intro()
            chosen_cave = choose_cave()
            enter_cave(chosen_cave)

            print("\n\nDo you want to try again? (yes or no)")
            playGameAgain = input()
            print("You said: " + playGameAgain)
            time.sleep(1)

            if playGameAgain == "yes" or playGameAgain == "y":
                print("\nLet's try again!")
                
            else:
                print("\nOk, see ya later!")


main_loop()      

Link to post
Share on other sites

Well I finally figured it out :D

I know all through the course Stefan had stressed time and time again about the importance of spaces in Python and I did believe him I just hadn't really learnt the lesson very well until now.

So I was determined not to be beaten by this problem with my code, but no matter how many times I looked at my code and compared it to the source code I just couldn't see my fault.

Not wanting to move onto the next lesson without fixing my code I decided I'd have to keep checking and re checking until I found my mistake.

There it was as plain as day........ SPACING! how could I miss that?? :rolleyes:

Well I'm not entirely sure how I missed it, but I'm glad i did, I've learnt a very valuable lesson today :D

 

 

For comparisons sake heres my newly spaced code:

 

# Python treasure hunt game


import random, time

def display_game_intro():
    print('''

           ---->Welcome to the 'Python Treasure Hunt Game'
           ...the most amazing game ever made!

           After a long journey, you find yourself in front of two caves.
           One cave leads to a treasure, the other, a spike filled pit!
           Being brave, and a little greedy for treasure, you've decided
           to go for it!
       ''')


def choose_cave():
    cave = ''
    while cave != '1' and cave != '2':
        print('Which cave do you want to enter? (1 or 2)')
        cave = input()
    return cave


def enter_cave(chosen_cave):
    print('\nYou have entered a cave...')
    time.sleep(1)

    random_cave = random.randint(1,2)
    #print("random_cave= " + str(random_cave))

    if random_cave == int(chosen_cave):
        print("---> Lucky you! You found the chest!")
    else:
            print("--->You expected to find a chest\n ...and all you found was DEATH!")

            
    


def main_loop():

    ''' The main_loop() function controls the flow of the game by
calling functions and using conditionals'''

    playGameAgain = "yes"

    while playGameAgain == "yes" or playGameAgain == "y":
        
            display_game_intro()
            chosen_cave = choose_cave()
            enter_cave(chosen_cave)

            print("\n\nDo you want to try again? (yes or no)")
            playGameAgain = input()
            print("You said: " + playGameAgain)
            time.sleep(1)

            if playGameAgain == "yes" or playGameAgain == "y":
                print("\nLet's try again!")

            else:
                print("\nOk, see ya later!")


main_loop()

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...