0

I'm working on a basic game. I'd like to know how I can make the code go to a specific point in the code/if-statement, rather than starting off at the beginning.

For example, in my code at line 19: kithcen() I don't want to redirect it to the start of the if-statement; again going on the describe the kitchen and then asking for an input: choice02 = raw_input("What do you do?"), but rather i want it to directly skip to the input part.

def kitchen():
 print "You see a dusty old kitchen, nobody has probably used it for quite a while."
 print "There are 3 different cupboards(0, 1, 2) or a letter(3) placed on the counter."
 choice02 = raw_input("What do you do? >")
 if choice02 == "0" or choice02 == "first cupboard" or choice02 == "check first cupboard" or choice02 == "open first cupboard":
 print "You see a dusty empty bottles of milks, with spiderwebs at the corners of the cupboard. Nothing of Interest here."
 raw_input(">")
 kitchen()
 elif choice02 == "1" or choice02 == "second cupbaord" or choice02 == "check second cupboard" or choice02 == "open second cupboard":
 print "You see some packs of likely expired cereal and a key(0)."
 choice_02_A = raw_input("What do you do? >")
 #----------------------------------------------------------------------------------------------------------#
 if choice02_A == "pick key" or choice02_A == "key" or choice02_A == "0" or choice02_A == "get key":
 print "You picked the key. This probably unlocks some door."
 raw_input("> ")
 kitchen()
 elif choice02_A == "close cupboard" or choice02_A == "kitchen" or choice02_A == "go back to kitchen":
 kitchen()
 else:
 print "Not a valid option."
 #-----------------------------------------------------------------------------------------------------------#
 elif choice02 == "2" or choice02 == "third cupboard" or choice02 == "check third cupboard" or choice02 == "open third cupboard":
 print "You see an empty or dusty cupboard. Nothing of interest here."
 raw_input("> ")
 kitchen()
 elif choice02 == "3" or choice02 == "check letter" or choice02 == "letter" or choice02 == "read letter":
 print """
 You read the letter:
 \n"Dear Shawn............\n"
 It makes no sense to you.
 """
 elif choice02 == "go back" or choice02 == "entrance hall" or choice02 == "go to entrance hall":
 entrance_hall()
else:
 "Not a valid Option."
asked May 31, 2017 at 21:32
4
  • Indent your code properly Commented May 31, 2017 at 21:35
  • Also, you keep saying "loop" in your question. What loop? Commented May 31, 2017 at 21:35
  • Sorry, my bad, I'm new. A misconception, anyways I made a edit. The indentation go messed up a bit while copying. Commented May 31, 2017 at 21:45
  • No worries. Also, please indent. The code as it stands is not valid Python. Specifically, everything after def needs to be up one level. Commented May 31, 2017 at 21:48

2 Answers 2

1

I see no loop, but if you only want the print statements when you call the function from outside, you could have an optional boolean like this:

def kitchen(already_there=False):
 if not already_there:
 print('...')
 choice02 = raw_input("What do you do? >")
 # ...
 elif # ...
 # ...
 kitchen(already_there=True)
answered May 31, 2017 at 21:39
Sign up to request clarification or add additional context in comments.

1 Comment

This is especially nice because it is backwards compatible with any code you've already written.
0

You might want to consider refactoring your code and using dictionaries to avoid long if-else statements.

See https://stackoverflow.com/a/27365077/2884613.

print "There are 3 different cupboards(0, 1, 2) or a letter(3) placed on the counter." 
alias = {
 '0': '0',
 'first cupboard': '0',
 'check first cupboard': '0',
 'open first cupboard': '0',
 '1': '1',
 'second cupboard': '1',
 'check second cupboard':'1',
 ...
}
dic = {
 "0":"You see a dusty empty bottles of milks, with spiderwebs at the corners of the cupboard. Nothing of Interest here.", \
 "1":"You see some packs of likely expired cereal and a key(0).", \
 "2":"You see an empty or dusty cupboard. Nothing of interest here."
 ...
}
while(True):
 choice02 = raw_input("What do you do? >")
 if choice02 in alias:
 print dic[alias[choice02]]
 break
 else:
 print "Not a valid option"
answered May 31, 2017 at 21:52

2 Comments

Alright, Thanks! This is definitely cleaner. Though how could I assign multiple keys to a single value/string
Hmm, one possibility is to use two dictionaries. One to store the alias key association and one to store your actual data. I'll update my answer shortly.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.