Improving my code


#1

Hi there.

Is there any way I can improve my code here?

import sys
import time

#Defining the players inventory and the items
inventory = ()
candle = ('candle',)

#Gameloop without the rules and warning and welcome message
def game_loop1():
    first_choice()
    return

#The welcome message
def welcome():
    print(''' ______  _____  _____          _____  ______   _______ _    _ ______
|  ____|/ ____|/ ____|   /\   |  __ \|  ____| |__   __| |  | |  ____|
| |__  | (___ | |       /  \  | |__) | |__       | |  | |__| | |__
|  __|  \___ \| |      / /\ \ |  ___/|  __|      | |  |  __  |  __|
| |____ ____) | |____ / ____ \| |    | |____     | |  | |  | | |____
|______|_____/ \_____/_/    \_\_|    |______|    |_|  |_|  |_|______|


 __  __          _   _  _____ _____ ____  _   _
|  \/  |   /\   | \ | |/ ____|_   _/ __ \| \ | |
| \  / |  /  \  |  \| | (___   | || |  | |  \| |
| |\/| | / /\ \ | . ` |\___ \  | || |  | | . ` |
| |  | |/ ____ \| |\  |____) |_| || |__| | |\  |
|_|  |_/_/    \_\_| \_|_____/|_____\____/|_| \_|''')
print('''\nWelcome to Escape the Mansion!!!''')

#The rules of the game
def rules():
    print('\nHere are the rules:')
    print('''\n1. Enter the number behind the choice that you want to make to make that choice.
               2. Don't Cheat
               3. Read Carefully. Some things come back to bite you!''')

    return

#Generator issues
def warning():
    print('')
    print('')
    print('\nWarning! We are experiencing generator issues. If you hear alarm bells going off it might be due to this.')
    return


def first_choice():
    print('')
    print('''\nYou find yourself in a dark musty room.
              You do not know where you are, or what happended to you.
               You are aware that there is a dim light behind you.
                You can either:

              1. Go through the door in front of you.You
              2. Go through the door on your left.
              3. Turn Behind you''')
    return



def turn_behind():
    print('''You can now either:

           1. Go through the door in front of you.
           2. Go through the door on your left.''')
    return


def closet():
    print('''\nYou can now either:

              1. Go through the door in front of you
              2. Go through the door on your left
              3. Turn behind you''')
    return


def down_stairs():
    print('You are now downstairs')
    print('''\nYou can either:

                1. Go through a hallway to your right where you see a rack of knives and the sound of barking dogs
                2. Go through a small passageway to your left ''')
    return


def passage_left():
    print('You discover a person that has been hanged...')
    print('')
    print('''\nYou can either:

                1. Cut the rope he is hanging from
                2. Leave him alone''')
    return


welcome()
rules()
warning()

first_choice()

ch1 = input('\n>>>')

if ch1 == ('1'):
    print('You fall down a dark staircase and die')
    sys.exit(0)

elif ch1 == ('2'):
    print('You just enter a dark closet')
    closet()
clzt = input('\n>>>')
    #for if you enter dark closet

if clzt == ('1'):
 print('You fall down a dark staircase and die')

elif clzt == ('2'):
    print('A spirit gets angry that you keep entering the closet and kills you')
    input('\nPlease press any key to exit...')
    sys.exit(0)

elif clzt == ('3'):
    print('You turn behind you and find a candle')
    inventory += candle
    print('Your inventory now contains,',candle)

#Turn behind you and find a candle
elif ch1 == ('3'):
    print('You turn behind you and find a candle')
    inventory += candle
    print('Your inventory now contains,',inventory,)
    turn_behind()

else:
    print('That isnt a valid choice')
    sys.exit(0)

#2

You've done all of this? Wow, good job, @boardmaster07581!
I find it almost impossible to write ESCAPE THE MANSION like that.

I'd be one of those guys who would play your game many times to check what would happen to me, I love this kind of code!

And I think you could also try to explore why there's a person hanging on a certain room, maybe even add a timer so the user wouldn't have the whole day to try to escape.
You should check time from the datetime module, it would help you!

Also, at certain points of your timer, you could also add someone or something that's chasing the user.
If the user can't find his/her way out and they ran out of time, this thing will get them and it will be game over.

I think calling sys.exit(0) is good at certain moments but maybe at a game, you'll just want to bring back the initial "screen", with the title of the game.
There the user could decide if he/she wants to play or not.

I also have other ideas like saving in a file how long it took the user to escape the mansion, how many times he/she failed and their name, of course.


#3

Hi there g4b3!

Thanks for all the wonderful tips! I will definitely implement them when I have time.

What I meant was is there a way to improve the code?


#4

Like what code should I type after you enter the dark closet?

Also, is it possible to put if/else statements into a function while defining it?

Thanks!


#5

That's actually up for you to decide what will happen next! Think about it first and later think about the code.

Yes, it's possible.
if/elif/else can be used either with or without functions.


#6

Do you have to type return at the end of defining a function?


#7

@boardmaster07581: Not all functions need to return a value, this happens when you just want to print something on a screen or anything else that doesn't involve bringing back a value to calculate something.


#8

Oh Okay!

Thanks so much g4be for your support I am very grateful


#9

No problem, Munching Fruit! Glad to help!