Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 24th, 2005, 12:09 PM   #1
Gunman
Programmer
 
Gunman's Avatar
 
Join Date: Oct 2005
Posts: 56
Rep Power: 3 Gunman is on a distinguished road
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 .
Gunman is offline   Reply With Quote
Old Oct 24th, 2005, 3:35 PM   #2
Silvanus
Hobbyist Programmer
 
Silvanus's Avatar
 
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 4 Silvanus is on a distinguished road
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?
Silvanus is offline   Reply With Quote
Old Oct 24th, 2005, 9:53 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Oct 24th, 2005, 11:02 PM   #4
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Oct 25th, 2005, 6:10 AM   #5
Gunman
Programmer
 
Gunman's Avatar
 
Join Date: Oct 2005
Posts: 56
Rep Power: 3 Gunman is on a distinguished road
Wtf..its so sensitive ? lol,..Anyway thanx for helping .
Gunman is offline   Reply With Quote
Old Oct 25th, 2005, 6:29 AM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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 is offline   Reply With Quote
Old Oct 25th, 2005, 6:34 AM   #7
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Old Oct 25th, 2005, 6:51 AM   #8
Silvanus
Hobbyist Programmer
 
Silvanus's Avatar
 
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 4 Silvanus is on a distinguished road
Yeah, I'm with Arevos- it's never been a problem. I love not having curly braces all over the place.
__________________
:wq
Silvanus is offline   Reply With Quote
Old Oct 25th, 2005, 4:47 PM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Nov 6th, 2005, 4:16 AM   #10
Ash Gotham
Newbie
 
Join Date: Nov 2005
Posts: 1
Rep Power: 0 Ash Gotham is on a distinguished road
elif and if must be in same indent if they are to recieve equal weightage
Ash Gotham is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:09 AM.

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