Variables, if elif and else statments combined together


#1

So hey, it's me again. I'm wondering if I can combine variables and if elif and else statements all together like in Javascript with variables and everything.

Javascript Example:

var user_input = prompt("I am going to ask you some questions to get to know you. Ready?");

if (user_input == "Yes") {
confirm("Welcome! Be sure to answer every question with honestly.");
prompt("Q I: How old are you?");
prompt("Q II: [This Is If You Friended Me. Skip past it if you didn't.] How'd you find me and what'd you find me for?");
prompt("Q III: [OPTIONAL] Where are you from?");
prompt("Q IV: [LAST QUESTION] Are you a coder?");
confirm("Confetti and confirm! You're done, but, before you go bonkers, please tell your answers to the sender of this website link, in order. Thank you for your compliance.");
}
else if (user_input == "No") {
confirm("Oh righty then, if you aren't then please press OK. It'll get you out, I promise.");
} else {
confirm("Invalid Answer. Make sure the Y or N is capitalized.");
}
Okay? It might have a few bugs, but you get the point. So, here I'm talking about Python and I'm going to do kind of a prototype text adventure game. Now, I'm stuck. I know I need variables for a certain item, in this case, a key. Now, what IF the key isn't in the player's inventory? They need to get the key.

Python Code:

def house():
print("Greetings and welcome! Please input your player name.")
name = input("name: ")
print("Your difficulty is based on how you play. Text based games are for people with a visual imagination, so there really is no difficulty.")
print("The game begins. You're in a mansion, and your family seems to have left. There is a fireplace lit up. There is a staircase, and oddly, there is a crack in the fireplace, just under the fire. ")
if answer == "look around" or answer == "look":
print("You look around and you see a lantern on a table. Along with it, there is 2 oil jars each filled half way with oil.")
elif answer == "go up staircase" or answer == "staircase":
print("You go up the staircase and you see two doors. Try the first door or the second door?")
def staircase():
if answer == "first door" or answer == "first":
#This is where I want to add a key, because this first door is locked. The player needs a key.
#What do I put here in order to combine variables, two print statements, and a conditional statement?
elif answer == "second door" or answer == "second":
else:
print ("")
staircase()

staircase()
else:
print ("Please pick a different answer.")
def #():
if answer == "#" or answer == "#":
elif answer == "#" or answer == "#":
else:
print ("")
#()

()

elif answer == "go in the cavehole" or answer == "go in":
    print ("You almost fall to your death! Luckily, you survive that steep fall.")
else:
    print ("Great, you've confused the game, now choose what answers are given to you! >:D")

house()

You get the idea, if you've read the comments in the middle of the first and second doors part. Sorry about the indentation, it's actually indented (almost) properly in codecademy labs. Might be a little confusing. Thank you!


#2

Here's a quick example I wrote up:

It uses lists, something you'll learn a bit later on in codecademy, but it can be used in the same way as you did in JavaScript.


#3

Excellent! Thanks dude, my father is kinda pushing me to do something with Python and I decided to do a text adventure game today, it's gonna be kinda short at first, but like I said, the first part of it is a prototype. Who knows? I might actually get it on a game site. Your help does a lot for me, so thanks.


#4

Text adventure games are a good place to start... Python is an excellent language to work with. Best of luck.


#5

I know, a great place to start! Look at Zork's evolution. Python is very basic, but can do a lot of stuff! It's my favourite to work with! Thank you for the support.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.