Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 03-13-2013, 02:11 PM   #1
Beta Member
 
Join Date: Mar 2013
Posts: 4
Default Python "if" problem

I have just started working on a program in Python that calculates various types of interest. Every time I try to run it, it tells me that there's an invalid syntax and it highlights the red "if". If I delete that line of code completely, it then tells me that "print" is invalid. If I delete that, it tells me that the "def" in front of "main()" is invalid, and so on and so forth....

I've never run into this problem before, and I'm very confused. I've rewritten the program a few different ways and each time it highlights something that it shouldn't, like a correctly written statement or variable. I assume this is something simple that I'm overlooking, but I'm completely baffled nonetheless.

Please help me figure out what's going on here.


Side note: I've run other programs I've made, and even wrote a short test program with similar functions to make sure the program or computer wasn't at fault and everything worked fine in all of the other programs. This leads me to believe something is wrong within the program.

I am somewhat new to programming so please speak in somewhat layman's terms for me.
Attached Images
File Type: jpg image-2832960814.jpg (53.7 KB, 15 views)
__________________

swinefire is offline   Reply With Quote
Old 03-13-2013, 02:44 PM   #2
In Runtime
 
Join Date: Feb 2013
Location: UK
Posts: 156
Default Re: Python "if" problem

You're missing your closing bracket on your 'timesComp' line - the raw input string is quoted inside the parentheses (correctly) but never terminated. The reason the interpreter tells you your error is on the next line is because it is expecting a ')' not an 'if'

Hope that helps.

p.s. since you're relatively new to programming - be careful with indentation in python, as you'll know it governs the 'scope' of all your objects (variables, functions etc.) so ensure you always use 'tab' rather than spaces, and make sure you use the same editor every time as different editors could use different size indentations for a tab character.
__________________

_michaelm is offline   Reply With Quote
Old 03-13-2013, 03:20 PM   #3
Beta Member
 
Join Date: Mar 2013
Posts: 4
Default

Ahh, I knew it would be something simple haha. I figured another set of eyes would spot it. Thanks for the help!
swinefire is offline   Reply With Quote
Old 03-13-2013, 05:30 PM   #4
Site Team
 
celegorm's Avatar
 
Join Date: Sep 2006
Posts: 10,713
Send a message via AIM to celegorm
Default Re: Python "if" problem

Quote:
Originally Posted by _michaelm View Post
and make sure you use the same editor every time as different editors could use different size indentations for a tab character.
That's actually the reason why I and most companies I've done work with require people to set their editors to put 4 spaces in place of a tab character. That way multiple people can work on a file and not have any issues of compatibility due to how one system interpreted the tab.
__________________
"as a fanboy i refuse to admit it and will pull countless things out of my butt to disprove it"

Team Thelegorm! Total Kills: 21 (i iz in uor profile, editsing your sigz)
celegorm is offline   Reply With Quote
Old 03-13-2013, 06:41 PM   #5
Site Team
 
berry120's Avatar
 
Join Date: Jul 2009
Location: England, UK
Posts: 3,425
Default Re: Python "if" problem

Quote:
Originally Posted by celegorm View Post
That's actually the reason why I and most companies I've done work with require people to set their editors to put 4 spaces in place of a tab character. That way multiple people can work on a file and not have any issues of compatibility due to how one system interpreted the tab.
This - use spaces rather than tabs. Pretty much all IDEs I know of are set to do this by default (the tab key inserts 4 spaces) for this reason, even if the language isn't reliant on indentation it still makes for much easier reading when you can open it anywhere and guarantee it'll look the same.
__________________
Save the whales, feed the hungry, free the mallocs.
berry120 is offline   Reply With Quote
Old 03-13-2013, 06:42 PM   #6
Beta Member
 
Join Date: Mar 2013
Posts: 4
Default

It isn't really an issue for me since I only work in IDLE. I'm taking an online programming class at school and all of the examples, etc. are in IDLE so I just stick with that. I didn't realize that different editors have different tab spacings though so that's a good bit of knowledge to know for later on.
swinefire is offline   Reply With Quote
Old 03-16-2013, 10:24 PM   #7
In Runtime
 
lhamil64's Avatar
 
Join Date: Jan 2007
Posts: 398
Default Re: Python "if" problem

One thing that I'd really recommend is learning how to take screenshots (I'm assuming you don't just based on the picture you attached, but if you do, feel free to ignore this post). You can usually just press the PrintScreen key on your keyboard (usually near the delete and insert keys, although you might have to hit the Fn key on a laptop to get to it). Then, if you're on Windows, you can open up Paint (or some other image editor) and paste in the image.

If you're on Windows Vista or later, you can use the Snipping tool instead (just search in the start menu for 'snip' and it should come right up).
__________________
--Lee
Website/Blog: http://lhamil64.wordpress.com
lhamil64 is offline   Reply With Quote
Old 03-17-2013, 02:08 PM   #8
In Runtime
 
Join Date: Feb 2013
Location: UK
Posts: 156
Default Re: Python "if" problem

Quote:
Originally Posted by swinefire View Post
Ahh, I knew it would be something simple haha. I figured another set of eyes would spot it. Thanks for the help!
No problem, glad to help.
Quote:
Originally Posted by celegorm View Post
That's actually the reason why I and most companies I've done work with require people to set their editors to put 4 spaces in place of a tab character. That way multiple people can work on a file and not have any issues of compatibility due to how one system interpreted the tab.
I completely agree. I've just had situations with freshly kicked installations and used vim without setting my usual environment - thought a lot of new kids on the block would fall foul sooner or later. Thankfully most of the proper editors take care of it for you, as you said.
__________________

_michaelm is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 11:31 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0