Jump to content

Help with basics

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
I learning python. And every time I use "elif" I get an error. Please help me. I struct here!
number = 23

guess = int(raw_input('Enter an integer : '))

if guess == number:

    print "Congradulations, you guessed it."

    print "(but you do not win any prizes!)"

    elif guess < number:

    print "Try higher number"

    else:

        print "Done"

    


C:\Python26>python mygirl.py

  File "mygirl.py", line 6

    elif guess < number:

       ^

SyntaxError: invalid syntax


#2
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
The 'elif' and 'else' statement should be on the same block level as the beginning 'if' statement. If you highlight the 'elif' and 'else' lines, and then choose 'Format' and 'Dedent Region' in IDLE that should fix your code with minimal mouse clicks.

Hope this helps.

-Cody-


isuru said:

I learning python. And every time I use "elif" I get an error. Please help me. I struct here!
number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
    print "Congradulations, you guessed it."
    print "(but you do not win any prizes!)"
    elif guess < number:
    print "Try higher number"
    else:
        print "Done"
    

Posted Image

#3
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
No, Then I get this error.

C:\Python26>python mygirl.py
  File "mygirl.py", line 4
    print "Congradulations, you guessed it."
        ^
IndentationError: expected an indented block



isuru said:

I learning python. And every time I use "elif" I get an error. Please help me. I struct here!
number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
    print "Congradulations, you guessed it."
    print "(but you do not win any prizes!)"
    elif guess < number:
    print "Try higher number"
    else:
        print "Done"
    

C:\Python26>python mygirl.py
  File "mygirl.py", line 6
    elif guess < number:
       ^
SyntaxError: invalid syntax


#4
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
    print "Congradulations, you guessed it."
    print "(but you do not win any prizes!)"
elif guess < number:
    print "Try higher number"
else:
    print "Done"

Worked for me.
Posted Image

#5
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
Because of you guide, I found the error.

highlight the 'elif' and 'else' lines, and then choose 'Format' and 'Dedent Region' in IDLE that should fix your code with minimal mouse clicks.


That is the method! Thanks! else and elif do not need indentions.

number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
    print "Congradulations, you guessed it."
    print "(but you do not win any prizes!)"
elif guess < number:
    print "Try higher number"
else:
        print "Done"


Root23 said:

number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
    print "Congradulations, you guessed it."
    print "(but you do not win any prizes!)"
elif guess < number:
    print "Try higher number"
else:
    print "Done"

Worked for me.


#6
Root23

Root23

    Programmer

  • Members
  • PipPipPipPip
  • 144 posts
Glad to help.
Posted Image