Python "if" problem

swinefire

Beta member
Messages
4
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.
 

Attachments

  • image-2832960814.jpg
    image-2832960814.jpg
    53.7 KB · Views: 15
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.
 
Ahh, I knew it would be something simple haha. I figured another set of eyes would spot it. Thanks for the help!
 
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.
 
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.
 
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.
 
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).
 
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.
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.
 
Back
Top Bottom