helpthepi
Posts: 54
Joined: Sat Apr 08, 2017 6:29 pm

else and elif syntax error [solved]

Mon Apr 10, 2017 2:27 pm

i was using python 3.4.2 today and while creating a program ran into the problem that else and elif was not defined. WHY! do not get it. here is the code i was using:
SCROLL DOWN FOR EDITED CODE

Code: Select all

RLG=input(" Welcome RLG Gamers is there a admin with you? Y or N")
if RLG== "Y":
  print("cool")
print("Admin(s) let the players answer the questions")  
elif RLG== "N":
  print("ok..")
print("that is fine but here are the rules")
else:
    print("sorry did not regonise answer")
EDITED CODE:

Code: Select all

RLG=input(" Welcome RLG Gamers is there a admin with you? Y or N")
if RLG== "Y":
  print("cool")
print("Admin(s) let the players answer the questions")  
elif RLG== "N":
  print("ok..")
print("that is fine but here are the rules")
else:
    print("sorry did not regonise answer")
Last edited by helpthepi on Mon Apr 10, 2017 3:05 pm, edited 2 times in total.
#LorienLegacies #BringBack8

Ernst
Posts: 1334
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: else and elif syntax error

Mon Apr 10, 2017 2:40 pm

I do not know much about python but I can see your mistake.
Take a close look and you should be able to identify the problem with your code.
The road to insanity is paved with static ip addresses

helpthepi
Posts: 54
Joined: Sat Apr 08, 2017 6:29 pm

Re: else and elif syntax error

Mon Apr 10, 2017 2:52 pm

Ernst wrote:I do not know much about python but I can see your mistake.
Take a close look and you should be able to identify the problem with your code.
I cant see anything
#LorienLegacies #BringBack8

User avatar
DougieLawson
Posts: 39126
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: else and elif syntax error

Mon Apr 10, 2017 2:55 pm

helpthepi wrote:i was using python 3.4.2 today and while creating a program ran into the problem that else and elif was not defined. WHY! do not get it. here is the code i was using:

Code: Select all

RLG=input(" Welcome RLG Gamers is there a admin with you? Y or N")
if RLG== "Y":
  print("cool")
print("Admin(s) let the players answer the questions")  
elif RLG== "N":
  print("ok..")
print("that is fine but here are the rules")
else:
    print("sorry did not regonise answer")
If that's an exact facsimile of your code then you've got some serious indentation problems.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: else and elif syntax error

Mon Apr 10, 2017 2:55 pm

Your indentation is incorrect.

Code: Select all

RLG=input(" Welcome RLG Gamers is there a admin with you? Y or N")
if RLG== "Y":
    print("cool")
    print("Admin(s) let the players answer the questions")  
elif RLG== "N":
    print("ok..")
    print("that is fine but here are the rules")
else:
    print("sorry did not regonise answer")
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

SkyRise
Posts: 179
Joined: Tue Jan 24, 2012 1:20 pm

Re: else and elif syntax error

Mon Apr 10, 2017 2:56 pm

Code: Select all

RLG=input(" Welcome RLG Gamers is there a admin with you? Y or N")
if RLG== "Y":
  print("cool")
  print("Admin(s) let the players answer the questions") 
elif RLG== "N":
  print("ok..")
  print("that is fine but here are the rules")
else:
  print("sorry did not regonise answer")
(Edit: the last line's indent was wrong too)
Last edited by SkyRise on Mon Apr 10, 2017 2:57 pm, edited 1 time in total.

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: else and elif syntax error

Mon Apr 10, 2017 2:57 pm

helpthepi wrote:
Ernst wrote:I do not know much about python but I can see your mistake.
Take a close look and you should be able to identify the problem with your code.
I cant see anything
Indentation. The second (and fourth) print statement ends the if block, so the elif after that will generate an error

Code: Select all

RLG=input(" Welcome RLG Gamers is there a admin with you? Y or N")
if RLG== "Y":
  print("cool")
  print("Admin(s) let the players answer the questions")  
elif RLG== "N":
  print("ok..")
  print("that is fine but here are the rules")
else:
    print("sorry did not recognise answer")
See e.g. http://www.python-course.eu/python3_blocks.php and googling for 'python indentation' will give you a lot of hits.

helpthepi
Posts: 54
Joined: Sat Apr 08, 2017 6:29 pm

Re: else and elif syntax error

Mon Apr 10, 2017 2:57 pm

scotty101 wrote:Your indentation is incorrect.

Code: Select all

RLG=input(" Welcome RLG Gamers is there a admin with you? Y or N")
if RLG== "Y":
    print("cool")
    print("Admin(s) let the players answer the questions")  
elif RLG== "N":
    print("ok..")
    print("that is fine but here are the rules")
else:
    print("sorry did not regonise answer")
I know it is wrong i am new to python!
how should the indentation be then
#LorienLegacies #BringBack8

helpthepi
Posts: 54
Joined: Sat Apr 08, 2017 6:29 pm

Re: else and elif syntax error

Mon Apr 10, 2017 3:01 pm

EDIT:
Edited the indentation still error message new code:

Code: Select all

RLG=input(" Welcome RLG Gamers is there a admin with you? Y or N")
if RLG== "Y":
  print("cool")
print("Admin(s) let the players answer the questions")  
elif RLG== "N":
  print("ok..")
print("that is fine but here are the rules")
else:
    print("sorry did not regonise answer")
#LorienLegacies #BringBack8

Ernst
Posts: 1334
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: else and elif syntax error

Mon Apr 10, 2017 3:04 pm

The road to insanity is paved with static ip addresses

helpthepi
Posts: 54
Joined: Sat Apr 08, 2017 6:29 pm

Re: else and elif syntax error

Mon Apr 10, 2017 3:12 pm

i have read it and edited code. Thank you. But a new problem is that it prints the else command even though it should do the if or elif. Here is new code:

Code: Select all

RLG=input(" Welcome RLG Gamers is there a admin with you? Y or N")
if RLG== "Y":
  print("cool")
  print("Admin(s) let the players answer the questions")  
elif RLG== "N":
  print("ok..")
  print("that is fine but here are the rules")
else:
    print("sorry did not recognise answer")
 
if i type in for the input Y it prints: sorry did not recognise answer
same with it if i enter N for the input.
#LorienLegacies #BringBack8

hippy
Posts: 7739
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: else and elif syntax error

Mon Apr 10, 2017 3:21 pm

helpthepi wrote:if i type in for the input Y it prints: sorry did not recognise answer
same with it if i enter N for the input.
Probably because you are pressing the Y or N key without holding down the shift key. That will return "y" or "n" not "Y" or "N".

There are a number of ways to force an uppercase character or to match for either upper or lowercase. One solution is ...

if RLG.upper() == "Y":
elif RLG.upper() == "N":

You can add a print command (function) to show what is actually in your RLG variable if that is not the problem.
Last edited by hippy on Mon Apr 10, 2017 3:24 pm, edited 1 time in total.

Ernst
Posts: 1334
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: else and elif syntax error

Mon Apr 10, 2017 3:23 pm

The road to insanity is paved with static ip addresses

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: else and elif syntax error

Mon Apr 10, 2017 3:27 pm

helpthepi wrote:I know it is wrong i am new to python!
how should the indentation be then
I posted your code with corrected indentation...

Ernst
Posts: 1334
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: else and elif syntax error

Mon Apr 10, 2017 3:39 pm

What I know about Python is little, with a little bit of effort I came up with this:

Code: Select all

RLG=raw_input(" Welcome RLG Gamers is there a admin with you? Y or N").upper()
if RLG=="Y":
  print("cool")
  print("Admin(s) let the players answer the questions")
elif RLG== "N":
  print("ok..")
  print("that is fine but here are the rules")
else:
  print("sorry did not recognise answer")
The road to insanity is paved with static ip addresses

helpthepi
Posts: 54
Joined: Sat Apr 08, 2017 6:29 pm

Re: else and elif syntax error

Mon Apr 10, 2017 3:43 pm

hippy wrote:
helpthepi wrote:if i type in for the input Y it prints: sorry did not recognise answer
same with it if i enter N for the input.
Probably because you are pressing the Y or N key without holding down the shift key. That will return "y" or "n" not "Y" or "N".

There are a number of ways to force an uppercase character or to match for either upper or lowercase. One solution is ...

if RLG.upper() == "Y":
elif RLG.upper() == "N":

You can add a print command (function) to show what is actually in your RLG variable if that is not the problem.
No i do use shift this is what comes in the shell

Code: Select all

Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1] on linux
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
 Welcome RLG Gamers is there a admin with you? Y or N Y
sorry did not recognise answer
>>> RLG
' Y'
>>> 
why does this happen?
#LorienLegacies #BringBack8

Ernst
Posts: 1334
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: else and elif syntax error

Mon Apr 10, 2017 3:46 pm

Do you notice the space character ?

Code: Select all

' Y'
The road to insanity is paved with static ip addresses

helpthepi
Posts: 54
Joined: Sat Apr 08, 2017 6:29 pm

Re: else and elif syntax error

Mon Apr 10, 2017 4:55 pm

Ernst wrote:Do you notice the space character ?

Code: Select all

' Y'
WOW. did not realise python was that sensitive.
it solved it. Thanks
#LorienLegacies #BringBack8

helpthepi
Posts: 54
Joined: Sat Apr 08, 2017 6:29 pm

Re: else and elif syntax error

Mon Apr 10, 2017 4:56 pm

how do i mark this post as solved
#LorienLegacies #BringBack8

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: else and elif syntax error

Mon Apr 10, 2017 8:38 pm

helpthepi wrote:
Ernst wrote:Do you notice the space character ?

Code: Select all

' Y'
WOW. did not realise python was that sensitive.
it solved it. Thanks
All languages are that sensitive. It isn't a python thing. "AB" is not equal to "B", why would " Y" be equal to "Y"?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: else and elif syntax error

Thu Apr 13, 2017 5:10 pm

Hello,
This is my two-bits.

Code: Select all

RLG = input(" Welcome RLG Gamers is there a admin with you? Y or N")
RLG = RLG.upper()
if RLG == "Y":
  print("cool \n Admin(s) let the players answer the questions")  
elif RLG== "N":
  print("ok.. \n that is fine but here are the rules")
else:
    print("Sorry did not recognise answer")

RGL.upper() will clean up the input error you may get if they type "y"
The "\n" is a new line.
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 13101
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: else and elif syntax error [solved]

Thu Apr 13, 2017 5:18 pm

marked post as solved.

Return to “Python”