-2

I am a beginner having some trouble with my e.g. code and cant figure out what is wrong with it

any help would be much appreciated!

answer = input("Do you want to play (yes/no)?\n>")
if answer .lower() .strip() == "yes":

    answer = input("Question 1, Option 1 - a Option 2 b Option 3 - c \n>")

    input(answer)

    if answer .lower() .strip() == "a":
        answer = input("Question 2.1, Option 1 - a Option 2 - b Option 3- c \n>")
    elif answer .lower() .strip() == "b":
        answer = input("Question 2.2, Option 1 - a Option 2 -b Option 3 - c \n>")
    else answer .lower() .strip() == "c":
    answer = input("Question2.3, Option 1 - a Option 2 - b Option 3 - c \n>")
        
else:   
    print =("Mabey next time!")

line 12 VS code is saying I need a ":" Infront of "answer" but I don't see why but if I do put I there then I apparently need an expression at the end of "c:". So how do I solve this and make the code work?

tkausl
  • 13,686
  • 2
  • 33
  • 50
soda bob
  • 9
  • 1

1 Answers1

0

your else block should not have any kind of condition after it, I would suggest just making that an elif. Also, Python is unique in that it relies on the spacing and tabbing of the code, you need to put a tab before your last answer.

answer = input("Do you want to play (yes/no)?\n>")
if answer .lower() .strip() == "yes":

    answer = input("Question 1, Option 1 - a Option 2 b Option 3 - c \n>")


    if answer .lower() .strip() == "a":
        answer = input("Question 2.1, Option 1 - a Option 2 - b Option 3- c \n>")
    elif answer .lower() .strip() == "b":
        answer = input("Question 2.2, Option 1 - a Option 2 -b Option 3 - c \n>")
    elif answer .lower() .strip() == "c":
        answer = input("Question2.3, Option 1 - a Option 2 - b Option 3 - c \n>")
        
else:   
    print("Mabey next time!")
  • 1
    there is inefficiencies — have you fix? also, print is all wrong — have you fix on that? – rv.kvetch Mar 20 '23 at 23:48
  • @rv.kvetch thanks, fixed the print as well I didn't notice that – danejcamacho Mar 21 '23 at 01:26
  • the `input(answer)` I believe can be removed. unless the goal here is to ask the user to confirm their choice (still not a good use case for a double input imo). – rv.kvetch Mar 21 '23 at 12:21
  • also, any thoughts about pulling out common logic into a variable? for example, in worst case (user enters `c`) the logic `answer .lower() .strip()` runs a total of 4 times, which I feel definitely can be avoided here. – rv.kvetch Mar 21 '23 at 12:22
  • Danejcamacho Thank for your response it go the code working! – soda bob Mar 22 '23 at 23:01
  • Rv.Kevtch the logic you are mentioning above was added so if someone typed "A", "a " or "A " it would still work. Is there a better way to do this – soda bob Mar 22 '23 at 23:04