Python text-based game [closed]

New to Python, learned a couple things. I'm trying to make a text based game and was told to come here for a review. Any tips would be appreciated to improve my code. I would like to expand this the more I learn.

import time
import random

def  game():
#intro text
print('******************************')
print('     Welcome to the game!')
print('*******************************')

time.sleep(1)

print('You awaken in a cave, unable to see. Darkness surrounds you.')
time.sleep(1)
print('\nSuddenly a small flickering light is seen in the distance.')
time.sleep(1)
print('\nyou get on your feet and decide wether you should head towards the light or wait where you are and hope help finds its way.')

#first question
def firstquestion():
while True:
print('\nDo you get up and see what could be the source of this light? \nOr do you wait?')
print('You begin to walk towards the light.')
break
print('you wait, no one comes and you die...')
break
else:
print('try again...')
#second question
def secondquestion():
while True:
print('\nThe light turns out to be a torch. Do you take this torch? enter y / n. ')
print('good choice, you pick up the torch and walk the out of the cave.')
break
break
else:
print('try again...')

game()
firstquestion()
secondquestion()
print()


closed as off-topic by πάντα ῥεῖ, l0b0, 200_success, Cody Gray, GraipherMar 9 at 8:37

This question appears to be off-topic. The users who voted to close gave these specific reasons:

• "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – l0b0, 200_success, Graipher
• "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – πάντα ῥεῖ, Cody Gray
If this question can be reworded to fit the rules in the help center, please edit the question.

• This is not working code. Please at least format it to be executable. – l0b0 Mar 8 at 22:44
• Welcome to Code Review. We would be glad to help you improve the code, but as per the rules in the help center, the code must work correctly before we can review it. Since the game continues after you "die", I would consider this to be broken code, and thus off-topic for Code Review. – 200_success Mar 9 at 2:52

As @l0b0 pointed out, you probably messed up your indents upon copying. Assuming they work:

First, looking at your question() functions, you can see a pattern, they prompt the user with a question. The user then has only two choices (binary like). If the user fails to enter a valid answer, they get prompted again. So why not create a function that gets passed the question and the binary answers? This also allows for easier expansion down the line like so:

def binary_question(question, answers):
while True:
print(question)
break
else:
print('try again...')

Questions = [
'\nDo you get up and see what could be the source of this light? \nOr do you wait?',
'\nThe light turns out to be a torch. Do you take this torch? enter y / n.',
]

{
'get up': 'You begin to walk towards the light.',
'I get up': 'You begin to walk towards the light.',
'wait': 'you wait, no one comes and you die...',
},
{
'y': 'good choice, you pick up the torch and walk the out of the cave.',
},
]

game()
for que, ans in zip(Questions, Answers):
binary_question(que, ans)


If you are not familiar with dictionaries or zip() look them up.

Second, you can allow for more choices by changing the user input strings to lower case and populating your Answers list to be only lower case. That way the user can enter "I get up" or "i get up", or "I GET UP", etc, and still get a valid response. Like so:

def binary_question(question, answers):
while True:
print(question)
break
else:
print('try again...')

Questions = [
'\nDo you get up and see what could be the source of this light? \nOr do you wait?',
'\nThe light turns out to be a torch. Do you take this torch? enter y / n.',
]

{
'get up': 'You begin to walk towards the light.',
'i get up': 'You begin to walk towards the light.',
'wait': 'you wait, no one comes and you die...',
},
{
'y': 'good choice, you pick up the torch and walk the out of the cave.',
},
]

game()
for que, ans in zip(Questions, Answers):
binary_question(que, ans)


As you can see, we made use of str.lower() function and wrote lowercase only answers in our Answers dict.

Third, I just realized that if the player dies in question one, question two still gets asked. You need a return value to your main loop so you can break in case you are dead:

def binary_question(question, answers):
while True:
print(question)
print('try again...')

Questions = [
'\nDo you get up and see what could be the source of this light? \nOr do you wait?',
'\nThe light turns out to be a torch. Do you take this torch? enter y / n.',
]

# can use named tuples here to make code more readable
{
'get up': ('You begin to walk towards the light.', True),
'i get up': ('You begin to walk towards the light.', True),
'wait': ('you wait, no one comes and you die...', False),
},
{
'y': ('good choice, you pick up the torch and walk the out of the cave.', True),
},
]

game()
for que, ans in zip(Questions, Answers):
alive = binary_question(que, ans)
if not alive:
break


The new boolean values in Answers[] signify if that answer kills the player. In this case, True = alive and False = dead.

• so would i be able to make the whole program in this structure. Listing the questions, the answers. having the values set and a break for a false answer. then later on as i learn more then expand it. – samcastaner Mar 9 at 19:29
• If you only want to ask these type of questions, then this may work for now. However, as you can imagine, creating a game, even a text based game, can get complex fairly quickly. – Perplexabot Mar 9 at 22:32