Computer Forums

Computer Forums (http://www.computerforums.org/forums/)
-   Programming (http://www.computerforums.org/forums/programming/)
-   -   Python "if" problem (http://www.computerforums.org/forums/programming/python-if-problem-221835.html)

swinefire 03-13-2013 01:11 PM

Python "if" problem
 
1 Attachment(s)
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.

_michaelm 03-13-2013 01:44 PM

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.

swinefire 03-13-2013 02:20 PM

Ahh, I knew it would be something simple haha. I figured another set of eyes would spot it. Thanks for the help!

celegorm 03-13-2013 04:30 PM

Re: Python "if" problem
 
Quote:

Originally Posted by _michaelm (Post 12125419)
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.

berry120 03-13-2013 05:41 PM

Re: Python "if" problem
 
Quote:

Originally Posted by celegorm (Post 12125427)
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.

swinefire 03-13-2013 05:42 PM

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.

lhamil64 03-16-2013 09:24 PM

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).

_michaelm 03-17-2013 01:08 PM

Re: Python "if" problem
 
Quote:

Originally Posted by swinefire (Post 12125423)
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 (Post 12125427)
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.


All times are GMT -5. The time now is 12:23 AM.

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