Python text-based game












-1












$begingroup$


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?')
Answer = input()
if Answer == 'get up' or Answer == 'I get up':
print('You begin to walk towards the light.')
break
elif Answer == 'wait':
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. ')
Answer = input()
if Answer == 'y':
print('good choice, you pick up the torch and walk the out of the cave.')
break
elif Answer == 'n':
print('well now your blind and dead...')
break
else:
print('try again...')

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









share|improve this question









New contributor




samcastaner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    This is not working code. Please at least format it to be executable.
    $endgroup$
    – l0b0
    6 hours ago






  • 1




    $begingroup$
    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.
    $endgroup$
    – 200_success
    2 hours ago
















-1












$begingroup$


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?')
Answer = input()
if Answer == 'get up' or Answer == 'I get up':
print('You begin to walk towards the light.')
break
elif Answer == 'wait':
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. ')
Answer = input()
if Answer == 'y':
print('good choice, you pick up the torch and walk the out of the cave.')
break
elif Answer == 'n':
print('well now your blind and dead...')
break
else:
print('try again...')

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









share|improve this question









New contributor




samcastaner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    This is not working code. Please at least format it to be executable.
    $endgroup$
    – l0b0
    6 hours ago






  • 1




    $begingroup$
    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.
    $endgroup$
    – 200_success
    2 hours ago














-1












-1








-1





$begingroup$


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?')
Answer = input()
if Answer == 'get up' or Answer == 'I get up':
print('You begin to walk towards the light.')
break
elif Answer == 'wait':
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. ')
Answer = input()
if Answer == 'y':
print('good choice, you pick up the torch and walk the out of the cave.')
break
elif Answer == 'n':
print('well now your blind and dead...')
break
else:
print('try again...')

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









share|improve this question









New contributor




samcastaner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




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?')
Answer = input()
if Answer == 'get up' or Answer == 'I get up':
print('You begin to walk towards the light.')
break
elif Answer == 'wait':
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. ')
Answer = input()
if Answer == 'y':
print('good choice, you pick up the torch and walk the out of the cave.')
break
elif Answer == 'n':
print('well now your blind and dead...')
break
else:
print('try again...')

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






python game






share|improve this question









New contributor




samcastaner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




samcastaner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 13 mins ago









Mast

7,53763788




7,53763788






New contributor




samcastaner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 10 hours ago









samcastanersamcastaner

4




4




New contributor




samcastaner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





samcastaner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






samcastaner is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    This is not working code. Please at least format it to be executable.
    $endgroup$
    – l0b0
    6 hours ago






  • 1




    $begingroup$
    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.
    $endgroup$
    – 200_success
    2 hours ago


















  • $begingroup$
    This is not working code. Please at least format it to be executable.
    $endgroup$
    – l0b0
    6 hours ago






  • 1




    $begingroup$
    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.
    $endgroup$
    – 200_success
    2 hours ago
















$begingroup$
This is not working code. Please at least format it to be executable.
$endgroup$
– l0b0
6 hours ago




$begingroup$
This is not working code. Please at least format it to be executable.
$endgroup$
– l0b0
6 hours ago




1




1




$begingroup$
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.
$endgroup$
– 200_success
2 hours ago




$begingroup$
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.
$endgroup$
– 200_success
2 hours ago










1 Answer
1






active

oldest

votes


















1












$begingroup$

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)
Answer = input()
if Answer in answers:
print(answers[Answer])
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.',
]

Answers = [
{
'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.',
'n': 'well now your blind and dead...',
},
]

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)
Answer = input()
Answer = Answer.lower()
if Answer in answers:
print(answers[Answer])
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.',
]

Answers = [
{
'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.',
'n': 'well now your blind and dead...',
},
]

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)
Answer = input()
Answer = Answer.lower()
if Answer in answers:
print(answers[Answer][0])
return answers[Answer][1]
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
# read about it if interested.
Answers = [
{
'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),
'n': ('well now your blind and dead...', False),
},
]

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.






share|improve this answer











$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    samcastaner is a new contributor. Be nice, and check out our Code of Conduct.










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215052%2fpython-text-based-game%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1












    $begingroup$

    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)
    Answer = input()
    if Answer in answers:
    print(answers[Answer])
    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.',
    ]

    Answers = [
    {
    '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.',
    'n': 'well now your blind and dead...',
    },
    ]

    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)
    Answer = input()
    Answer = Answer.lower()
    if Answer in answers:
    print(answers[Answer])
    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.',
    ]

    Answers = [
    {
    '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.',
    'n': 'well now your blind and dead...',
    },
    ]

    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)
    Answer = input()
    Answer = Answer.lower()
    if Answer in answers:
    print(answers[Answer][0])
    return answers[Answer][1]
    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
    # read about it if interested.
    Answers = [
    {
    '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),
    'n': ('well now your blind and dead...', False),
    },
    ]

    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.






    share|improve this answer











    $endgroup$


















      1












      $begingroup$

      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)
      Answer = input()
      if Answer in answers:
      print(answers[Answer])
      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.',
      ]

      Answers = [
      {
      '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.',
      'n': 'well now your blind and dead...',
      },
      ]

      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)
      Answer = input()
      Answer = Answer.lower()
      if Answer in answers:
      print(answers[Answer])
      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.',
      ]

      Answers = [
      {
      '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.',
      'n': 'well now your blind and dead...',
      },
      ]

      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)
      Answer = input()
      Answer = Answer.lower()
      if Answer in answers:
      print(answers[Answer][0])
      return answers[Answer][1]
      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
      # read about it if interested.
      Answers = [
      {
      '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),
      'n': ('well now your blind and dead...', False),
      },
      ]

      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.






      share|improve this answer











      $endgroup$
















        1












        1








        1





        $begingroup$

        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)
        Answer = input()
        if Answer in answers:
        print(answers[Answer])
        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.',
        ]

        Answers = [
        {
        '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.',
        'n': 'well now your blind and dead...',
        },
        ]

        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)
        Answer = input()
        Answer = Answer.lower()
        if Answer in answers:
        print(answers[Answer])
        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.',
        ]

        Answers = [
        {
        '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.',
        'n': 'well now your blind and dead...',
        },
        ]

        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)
        Answer = input()
        Answer = Answer.lower()
        if Answer in answers:
        print(answers[Answer][0])
        return answers[Answer][1]
        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
        # read about it if interested.
        Answers = [
        {
        '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),
        'n': ('well now your blind and dead...', False),
        },
        ]

        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.






        share|improve this answer











        $endgroup$



        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)
        Answer = input()
        if Answer in answers:
        print(answers[Answer])
        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.',
        ]

        Answers = [
        {
        '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.',
        'n': 'well now your blind and dead...',
        },
        ]

        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)
        Answer = input()
        Answer = Answer.lower()
        if Answer in answers:
        print(answers[Answer])
        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.',
        ]

        Answers = [
        {
        '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.',
        'n': 'well now your blind and dead...',
        },
        ]

        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)
        Answer = input()
        Answer = Answer.lower()
        if Answer in answers:
        print(answers[Answer][0])
        return answers[Answer][1]
        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
        # read about it if interested.
        Answers = [
        {
        '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),
        'n': ('well now your blind and dead...', False),
        },
        ]

        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 4 hours ago

























        answered 5 hours ago









        PerplexabotPerplexabot

        366




        366






















            samcastaner is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            samcastaner is a new contributor. Be nice, and check out our Code of Conduct.













            samcastaner is a new contributor. Be nice, and check out our Code of Conduct.












            samcastaner is a new contributor. Be nice, and check out our Code of Conduct.
















            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215052%2fpython-text-based-game%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python