Ask coding questions

← Back to all posts
Having trouble with an "elif" statement
GarretOgden (3)

Answered by PYer (3893) [earned 5 cycles]
View Answer
Comments
hotnewtop
GarretOgden (3)

@PYer If it's not too much trouble, would you be able to help?

PYer (3893)

@GarretOgden of course is there a repl link?

PYer (3893)

@GarretOgden Got it! you cant unindent code between elif statements. You did:

elif (choice4 == 1):
     print ("Mario is struck down by the fire ball, and princess peach returns to Bowser. GAME OVER!")
winlose = False
health = 0
lives = 0
enemys = 0
elif choice2 == 2:
  print ("You and Mario are struck by the deadly speeding Bullet, GAME OVER!")  
  winlose = False

You have to indent it since it is part of the statement. Otherwise it will cause an error.

if (choice4 == 2): 
    print ("You and Mario have Saved the princess and saved the day, CONGRATULATIONS!!")
    winlose = True
elif (choice4 == 1):
     print ("Mario is struck down by the fire ball, and princess peach returns to Bowser. GAME OVER!")
winlose = False
health = 0
lives = 0
enemys = 0
elif choice2 == 2:
  print ("You and Mario are struck by the deadly speeding Bullet, GAME OVER!")  
  winlose = False
health = 0
lives = 0
enemys = 2
#Create the first choice/the second path (right) for the user 
elif choice1 ==2:
   print ("You chose right, the army of Goombas is heading your way, what do you want to do?")

On lines 41 - 58, needs to be changed to:

if (choice4 == 2): 
    print ("You and Mario have Saved the princess and saved the day, CONGRATULATIONS!!")
    winlose = True
elif (choice4 == 1):
     print ("Mario is struck down by the fire ball, and princess peach returns to Bowser. GAME OVER!")
   winlose = False
   health = 0
   lives = 0
   enemys = 0
elif choice2 == 2:
  print ("You and Mario are struck by the deadly speeding Bullet, GAME OVER!")  
  winlose = False
   health = 0
   lives = 0
   enemys = 2
elif choice1 ==2:
   print ("You chose right, the army of Goombas is heading your way, what do you want to do?")

Make sure to indent all other places i may have missed.
Please mark this as the correct answer Sorry i was late, i was working on a game

GarretOgden (3)

@PYer Yes, thank you. Unfortunately it is still coming up as an error.

PYer (3893)

@GarretOgden can you fix it and then i can look?

PYer (3893)

@GarretOgden line 43 is indented an extra time. can you mark my answer as correct since it solves the problem?

GarretOgden (3)

@PYer Sure, sorry about that

GarretOgden (3)

https://repl.it/@GarretOgden/Super-Mario-Final-Draft
So, now I'm having trouble with it recognizing my name when I put it in.

Pythonier (315)

I think I may have fixed it: https://repl.it/@Pythonier/WanHighPoints
Your main problems were the indenting and including an extra spaces between stuff like print() and input() ex:

# should be written like
a = input("?")
print(a)

# not this:
a = input ("?")
print (a)

Hope this helps

You can copy and paste the code or fork it by changing anything in the code

PYer (3893)

@Pythonier False. Python ignores whitespace like that. You can see my commented answer below

GarretOgden (3)

@PYer So, I copied the correct code that you gave me. It is still not running.

GarretOgden (3)

@PYer It is now showing something with line 44

PYer (3893)

@GarretOgden line 43 is not indented

GarretOgden (3)

@PYer Just indented it, now it says that it should not be indented

PYer (3893)

@GarretOgden ALL ERRROS FIXED: You also need to use python 3 not python 2: https://repl.it/@PYer/InexperiencedLuxuriousTechnology

GarretOgden (3)

@PYer Almost there! For some reason, when you choose option two in the beginning it skips all of the other choices and goes straight to the Bowser choice from the first choice/path.

GarretOgden (3)

@PYer The program is supposed to ask the user which path they want. if they choose path 1 then they run into the Bullet Bill and then the Hammer Bro. and Finally to Bowser. The first path is listed as just choice1, choice2, exct. However, when the user chooses path two, they run into the army of Goombas, a Chain chomp, and then Bowser. Those choices are the alt choices.

PYer (3893)

@GarretOgden what part do you want it to go too? sorry i am late for responding

GarretOgden (3)

@PYer Ok, the program starts with asking the user for their name, they input it. Then it'll ask them which path they want to take, telling them what is on the left and what is on the right. If they input 2 for their choice (right) it is supposed to go to the elif choice1 ==2: print "You chose right, the army of Goombas is heading your way. What do you do?" and then it will take the user through the altchoice statements. Those being the Army of Goombas, the Chain Chomp, and finally to the fourth alternate choice with Bowser. Hope that kind of clarified it a little.

GarretOgden (3)

@PYer So, if the user chooses to go right (2), then the program should skip over the first set of choices and go to the elif choice1 == 2 statement.

GarretOgden (3)

@PYer I'm sorry, but what's that?

PYer (3893)

@GarretOgden we can work together on the same repl

GarretOgden (3)

@PYer Oh ok, sure! How would I start that?

PYer (3893)

@GarretOgden go to the side of your repl and the file names. then click multiplayer, and click start session. then send me a link

GarretOgden (3)

@PYer It's getting there! For some reason it is still skipping some steps/print statements. More specifically with the Chain Chomp.

PYer (3893)

@GarretOgden i got to leave i'll be back in about 3 hours

GarretOgden (3)

@PYer Hey, I'm so sorry to be getting back to you so late. I unfortunately had to run a few errands. But unfortunately due to a family thing that came up, I probably won't be able to work on it again until later tonight. The only thing that I need help fixing now are the alternate choices 3 & 4. The one with the Chain Chomp and Bowser.

GarretOgden (3)

@PYer Fixed the Chain Chomp section, now all that's left is the Army of Goombas!

GarretOgden (3)

@PYer Was able to fix the whole program! Thank you so much for your help, I really do appreciate it! :)

PYer (3893)

@GarretOgden sorry i cant work until monday

Steven_The_GuyT (305)

Here is a sample for python:

x = 0
if x == 1:
  print('x=1')
elif x == 0:
  print('x=0')
else:
  print('x doesn't equal 0 or 1')

This would print out:
x=1

GarretOgden (3)

I am currently working on a school project with Python and am having trouble with a certain elif statement on line 50 of my program. I've tried everything, from spacing to even just changing it to an if statement. Please help!

Vandesm14 (2644)

@GarretOgden Do you mind placing the code in a repl? Then the indentation won't be an issue

GarretOgden (3)

@Vandesm14 How would I do that to where you can see it? Sorry I'm still kind of new with repl.

Vandesm14 (2644)

@GarretOgden If you go to: https://repl.it/repls and click the "New Repl" at the top, select Python[2 or 3] from the language box and hit create, send me the url of that repl once it loads.

https://storage.googleapis.com/replit/images/1556835719375_f85a99d5b7690ae0897a2c9cf49170da.pn

https://storage.googleapis.com/replit/images/1556835754177_f91dd41ebe08b2dd275df9da90562aec.pn

Vandesm14 (2644)

@GarretOgden You don't need parentheses in the statement: elif a == b:

GarretOgden (3)

@Vandesm14 Is that what the problem was?

Vandesm14 (2644)

@GarretOgden If you remove the parentheses from the elif statements, it should fix your problem.

Please mark this as answered if I helped your problem 👍

GarretOgden (3)

@Vandesm14 Just removed the parentheses from all the elif statements, it's still popping up with line 50 in specific. Should I do it with all the if statements as well?

Vandesm14 (2644)

@GarretOgden @PYer I cannot help him past this point as I am not good at python

GarretOgden (3)

@Vandesm14 Oh ok, well thank you so much for your help. I really appreciate it!

PYer (3893)

@Vandesm14 is there still more help needed?

GarretOgden (3)

@PYer Yes please, I still can't seem to fix the issue with that elif statement. Any chance it's something else?

Vandesm14 (2644)

Can you post the link to the repl?

GarretOgden (3)

@Vandesm14 I've tried but since the project is a notepad file I don't think it will take it.

ReplTalk (2518)

Hey there, please make sure your post body is at least 10 words and 100 characters. That makes it easier for our community to help you!