BadCoder
BadCoder

Reputation: 33

How do I add a while loop to an if statement for my multiple choice story?

I'm trying to add a while loop in my if statement that is attached to another while loop. I'm unsure where I'm going wrong. I'm trying to learn Python on my own so I don't know very much.

I get an error message saying "unexpected character after line continuation character. It highlights my last 'if' statement right after the first quotation mark. If I were to take it out, it will highlight the last while True statement.

The # you see are from another post.

Basically, my question is how do I fix the next while loop statements for my story? And is the process the same for the future multiple choices I do?

while True:
    d1a = input ("Which do you inspect:\na) The back door?\nb) The basement?\n")
    # check if d1 is equal to one of the strings, specified in the list
    if d1a in ['a', 'b']:
        # if it was equal - break from the while loop
        break

# process the input
if d1a == "a": 
    print ("You approach the door.\n\
'Who's out there?'\n\
No one answers.\n\
You start to creep back into the kitchen but then there's tapping on the window.\n\
'Who's there? I'm warning you!'")
    while True:
        d2a = input ("What do you do:\na) Run outside to see who's there?\n\
b) Run back to your bedroom and hide underneath your bed?"\n)
        if d2a in ['a', 'b']:
            break

if d2a == "a":
    print ("You run out the door with a knife from the kitchen.\n\
You swing your head back and forth but see no one outside.")

elif d2a == "b":
    print ("You run up the stairs.\n\
There is a feeling of someone's hand on your back.\n\
It makes you run faster, not looking back.")


elif d1a == "b": 
    print ("You approach the basement.\n\
You go to turn on the light but it's flicking.\n\
You walk down the stairs. It's dim.\n\
You trip!\n\
'Ugh...'\n\
There's rustling under on the couch but you can't see what's on it.")
    while True:
        d2b = input ("What do you do:\na) Flash your flashlight on the couch?\n\
b) Ignore it and head back upstairs?")
        if d2b in ['a', 'b']:
            break

Upvotes: 3

Views: 194

Answers (3)

David Mayes
David Mayes

Reputation: 367

In python it's very important you get the indentations right and the scope of variables.

  • The very first 'break' is incorrectly indented. One more tab required.
  • The \n in d2a option b in outside the double quotes.
  • The if statements for the d2a response are incorrectly indented. Shift them out one more tab.

I've tidied the code up a bit here. Note: I've put double quotes around each line of text to be printed. A bit easier to look at.

while True:
    d1a = input ("Which do you inspect:\n"\
                 "a) The back door?\n"\
                 "b) The basement?\n")

    # check if d1 is equal to one of the strings, specified in the list
    if d1a in ['a', 'b']:
        # if it was equal - break from the while loop break
        break

# process the input
if d1a == "a": 
    print ( "You approach the door.\n" \
            "'Who's out there?'\n" \
            "No one answers.\n" \
            "You start to creep back into the kitchen but then there's tapping on the window.\n" \
            "'Who's there? I'm warning you!'")
    while True:
        d2a = input ("What do you do:\n" \
                     "a) Run outside to see who's there?\n" \
                     "b) Run back to your bedroom and hide underneath your bed?\n")
        if d2a in ['a', 'b']:
            break

    if d2a == "a":
        print ("You run out the door with a knife from the kitchen.\n" \
               "You swing your head back and forth but see no one outside.")

    elif d2a == "b":
        print ("You run up the stairs.\n" \
               "There is a feeling of someone's hand on your back.\n" \
               "It makes you run faster, not looking back.")


elif d1a == "b": 
    print ("You approach the basement.\n" \
           "You go to turn on the light but it's flicking.\n" \
           "You walk down the stairs. It's dim.\n" \
           "You trip!\n" \
           "'Ugh...'\n" \
           "There's rustling under on the couch but you can't see what's on it.")

    while True:
        d2b = input ("What do you do:\n"\
                     "a) Flash your flashlight on the couch?\n" \
                     "b) Ignore it and head back upstairs?")
        if d2b in ['a', 'b']:
            break

Upvotes: 1

gilmatic
gilmatic

Reputation: 1864

the new line character (\n) on line 17 needs to be included in the string (ie. it needs to be inside the quotations) - that is what is causing that particular error message.

In addition the break statement on line 6 needs to be indented. Other than that it should work - note that you need to type 'a' into the terminal as input in order to get it to work - you could use raw_input instead and then you can just type a

while True:
    d1a = raw_input ("Which do you inspect:\na) The back door?\nb) The basement?\n")
    # check if d1 is equal to one of the strings, specified in the list
    if d1a in ['a', 'b']:
        # if it was equal - break from the while loop
        break

# process the input
if d1a == "a":
    print ("You approach the door.\n\
'Who's out there?'\n\
No one answers.\n\
You start to creep back into the kitchen but then there's tapping on the window.\n\
'Who's there? I'm warning you!'")
    while True:
        d2a = raw_input ("What do you do:\na) Run outside to see who's there?\n\
b) Run back to your bedroom and hide underneath your bed?\n")
        if d2a in ['a', 'b']:
            break

if d2a == "a":
    print ("You run out the door with a knife from the kitchen.\n\
You swing your head back and forth but see no one outside.")

elif d2a == "b":
    print ("You run up the stairs.\n\
There is a feeling of someone's hand on your back.\n\
It makes you run faster, not looking back.")


elif d1a == "b":
    print ("You approach the basement.\n\
You go to turn on the light but it's flicking.\n\
You walk down the stairs. It's dim.\n\
You trip!\n\
'Ugh...'\n\
There's rustling under on the couch but you can't see what's on it.")
    while True:
        d2b = raw_input ("What do you do:\na) Flash your flashlight on the couch?\n\
b) Ignore it and head back upstairs?")
        if d2b in ['a', 'b']:
            break

Upvotes: 0

nosklo
nosklo

Reputation: 223172

You probably have a invisible space after the \ in the end of one of the lines above. I suggest that instead of using continuation characters, you just close the string and open it again in next line, it is less fragile and works better:

d2b = input ("What do you do:\na) Flash your flashlight on the couch?\n”
    ”b) Ignore it and head back upstairs?")

Upvotes: 0

Related Questions