Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop adventure game
#1
i am trying to get the loop
to do if user press l go back to previous line
I've looked on the internet and can't find anything that did not pertain to math or advanced projects
if choice=='s':
  print("ok you've choosen  the sword. You see two doors. one is to the left and one is to the right")
  doors=input('Which door do you want to open? l or r ')
 if doors =='r':
     print('you are in an empty room. All there is you and your weapon of choice  ')
 elif doors  =='l':
     print("what are you doing here get out!"",""Some one yells. And punches you and stabs you. You have lost a live")
     lifes=lifes-1
     print('You now have a life total of: ' + int(lifes)

and if I do while True:. it will give me nothing
and now i am getting an error on line 21
Error:
Traceback (most recent call last): File "main.py", line 21, in <module> print('You now have a life total of: ' + lifes) TypeError: can only concatenate str (not "int") to str
and here is out put for when i don't have the error and i use while true
Output:
Welcome to the fart monster game ---------------------------------- What is your name d ok d here are the controls: g(grab),l(left),r(right),u(up),d(down) press n to start n ---------------------------------------------- You are thrown into a dark room. All you have is a knife and a rope tied around on your wrists and you take the the knife and cut the rope and you find the light switch and turn it on and see a sword and a dagger Which one do you want? s(sword) or d(dagger) s ok you've choosen the sword. You see two doors. one is to the left and one is to the right Which door do you want to open? l or r l what are you doing here get out!,Some one yells. And punches you and stabs you. You have lost a live >
Reply
#2
You could either loop here until you get an answer you want, or you might prefer to push it off to another function. If you will have several locations in the code, I'd much rather have it in a separate function.

def get_specific_input(prompt, valid_responses):
    response = input(prompt)
    while response not in valid_responses:
        print(f"Please enter only one of {','.join(valid_responses)}."
        response = input(prompt)
    return response
Here the while loop will continue to prompt until the response matches one of the valid responses. Since it has already done the checking, your main code can just assume that one of the expected choices will be present.

Then you could call it like:
if choice=='s':
  print("ok you've choosen  the sword. You see two doors. one is to the left and one is to the right")
  doors=get_specific_input('Which door do you want to open? l or r ', ['l', 'r'])
 if doors =='r':
jefsummers likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  While loop not ending (Best of 10 dice game) K3nidi 3 1,498 Jul-09-2022, 09:53 AM
Last Post: K3nidi
  For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game new_coder_231013 7 2,263 Dec-28-2021, 11:32 AM
Last Post: new_coder_231013
  how to make a hotkey for text adventure game myn2018 2 1,974 Jan-06-2021, 10:39 PM
Last Post: myn2018
  Choose your own adventure game noahc2004 2 2,581 Jun-26-2020, 02:06 PM
Last Post: DPaul
  Designing a "game" loop api pitosalas 2 2,482 Jun-07-2020, 03:20 PM
Last Post: pitosalas
  Waiting in a text adventure StickyLizard 1 42,704 Jan-19-2019, 10:45 PM
Last Post: ichabod801
  Errors in simple text adventure game? ecloev 5 4,870 Apr-10-2018, 05:55 PM
Last Post: nilamo
  loop doesn't advance-py3 text game foxtreat 2 3,571 Jun-08-2017, 06:41 AM
Last Post: foxtreat
  Need a little more help in a Choose Your Own Adventure Program Goldberg291 13 18,512 Jan-31-2017, 08:33 AM
Last Post: Ofnuts
  Python Text Adventure sneakyninja0129 2 26,071 Jan-01-2017, 09:18 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020