Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Syntax error (http://www.programmingforums.org/showthread.php?t=6589)

Gunman Oct 24th, 2005 1:09 PM

Syntax error
 
Hey can someone help me correct this code .

This is the part where it says there is a sytax error .
:

elif guess < number:
            print "Guess is smaller than number"


And this is the full code .
:

#This program uses the if else if program to create the high low game

number = 30
guess  = 0

while guess!=number:                            #while guess!=number its detonated
    guess = input("Guess : ")                  #as the guess again .

    if guess > number:
        print "Guess is greater than number"

        elif guess < number:
            print "Guess is smaller than number"

            print "Just right!"


Thanx for help .

Silvanus Oct 24th, 2005 4:35 PM

Indentation matters. The "elif" needs to be at the same indentation level as the "if."

Don't you hate it when you get nailed by something like that?

Sane Oct 24th, 2005 10:53 PM

You'll also want to add in an "else" before the final line of code, or else :
print "Just right!" ,
will execute when :
print "Guess is smaller than number" ,
does.

:

#This program uses the if else if program to create the high low game

number = 30
guess  = 0

while guess!=number:            # while guess!=number its detonated
    guess = input("Guess : ")    # as the guess again

    if guess > number:
        print "Guess is greater than number"
    elif guess < number:
        print "Guess is smaller than number"
    else:
        print "Just right!"


I believe that's what you want.

bl00dninja Oct 25th, 2005 12:02 AM

i think indentation is important for readability and style...but for partitioning code blocks? just switching text editors could fuck up your program. that's stupid. i never got that about python.

Gunman Oct 25th, 2005 7:10 AM

Wtf..its so sensitive ? lol,..Anyway thanx for helping .

Arevos Oct 25th, 2005 7:29 AM

Quote:

Originally Posted by bl00dninja
i think indentation is important for readability and style...but for partitioning code blocks? just switching text editors could fuck up your program. that's stupid. i never got that about python.

It enforces good writing style. If you mix tabs and spaces for indentation, your program is going to look messed up on other people's machines if they have different tab sizes. Python prevents the user from mixing tabs and spaces for indentation in the same block. Since this is what you should be doing anyway, I'm not sure what the problem is.

Personally, I've never had any problems with Python indentation.

Arevos Oct 25th, 2005 7:34 AM

Quote:

Originally Posted by Gunman
Wtf..its so sensitive ? lol,..Anyway thanx for helping .

The reason behind it is that Python uses indentation to delimit blocks. So this code:
:

    if guess > number:
        print "Guess is greater than number"

        elif guess < number:
            print "Guess is smaller than number"

            print "Just right!"

Is equivalent to this Java code:
:

    if (guess > number) {
        System.out.println("Guess is greater than number");

        else if (guess < number) {
            print "Guess is smaller than number"

            print "Just right!"
        }
    }

The "else if" block is inside the "if" block, when it should come after.

Silvanus Oct 25th, 2005 7:51 AM

Yeah, I'm with Arevos- it's never been a problem. I love not having curly braces all over the place.

Dietrich Oct 25th, 2005 5:47 PM

Somebody is forgetting their ;;;;;;;;;;; in the Java code! Not only is Java loaded with {}{}{}{}{}, but also with ;;;;;; ! I prefer indentations, have to do it anyway so people can read the code!

Always thought that was the neatest thing about Python.

Ash Gotham Nov 6th, 2005 5:16 AM

elif and if must be in same indent if they are to recieve equal weightage


All times are GMT -5. The time now is 8:39 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC