-4

With this code I am trying to generate simple multiplication tables. The program should ask for input and multiple that number in a range up to 15 and the generate the multiplication table for the number. After the if_name_ == 'main': line I end up with a syntax error after the colon. I normally program in python 2, so python 3 is a bit new to me but I'm not sure what the difference is. Below I have listed the short but full code. Any help would be much appreciated.

'''Multiplication Table'''

def multi_table(a):
    for i in range(1,16):
        print(' {0} x {1} = {2} '.format(a, i, a*i))



if_name_ == '_main_':
    a = input('Enter a number: ')
    multi_table(float(a))
Bbrown
  • 13
  • 1
  • 1
  • 3

3 Answers3

4
if_name_ == '_main_':
    a = input('Enter a number: ')
    multi_table(float(a))

should be :

if __name__ == "__main__":
    a = input('Enter a number: ')
    multi_table(float(a))

Notice that both variable __name__ and __main__ has two underscores around them and that there must be a space between the if keyword and the start of the condition.

Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
2

As @Maroun Maroun said right, it has to be if __name__ == "__main__" . But you wont need it. Just write it at the bottom :

'''Multiplication Table'''

def multi_table(a):
    for i in range(1,16):
        print(' {0} x {1} = {2} '.format(a, i, a*i))

a = input('Enter a number: ')
multi_table(float(a))

Should work, too.

EDIT: In the official docs :

https://docs.python.org/3/library/main.html

if __name__ == "__main__":

Luatic
  • 8,513
  • 2
  • 13
  • 34
  • 1
    The downside of this is that if you later want to `import` the `multi_table` function for use somewhere else, it will block on input. See e.g. [*"What does `if __name__ == “__main__”:` do?"](http://stackoverflow.com/q/419163/3001761). – jonrsharpe Jan 15 '17 at 15:21
  • Thats true, @jonrsharpe , but if it should be a library, he wouldnt have put it in the same file as the input – Luatic Jan 15 '17 at 15:23
  • So it did work just fine without that line, but even when I did change the line to have two underscores I still had a syntax error. I'm trying to understand why. – Bbrown Jan 15 '17 at 15:27
  • Probably your editor(for example geany) works wrong ? – Luatic Jan 15 '17 at 16:11
  • 1
    @Bbrown Did you carefully check that there were two underscores before _and_ after both `name` and `main`? And you did remember to add the space after `if` too, yes? If your new version is still not working, perhaps paste it in as another example of a non-working syntax, in your question text. The SyntaxError was because of the missing space; the other two problems would have created different errors (such as NameError for `_name_`). – Dan Lowe Jan 15 '17 at 17:09
  • @DanLowe It was due to not having a space after if. Thank you, something so simple yet, so maddening. lol – Bbrown Jan 15 '17 at 20:14
0

In the example provided, (i) there's a space missing after if, and (ii) the symbol's name should have two pairs of underscores: __name__. If these two corrections resolve your problem, great.

But in case you still have another more mysterious unresolved syntax error, then read on...

Recently I encountered a very similar error message, and it was hard to root-cause, since the error message seems to point in the wrong direction. The error seems to indicate that the syntax error lies at the end of the if statement. However, with Python 3.x I noticed that in case a parentheses mismatch error happens in a python source file, the error might be reported at the beginning of the next block rather than where the mismatch actually occurs.

It's worth checking for mismatched parentheses in the entire source code file, prior to the if statement. Check if all your (s are matched by a closing ).

Kosa
  • 57
  • 5
  • 1
    This fixed my issue. I was assuming the IDE or interpreter would have flagged a missing parenthesis so I didn't bother to check. But hey, these things happen. – BareMetalCoder Jan 14 '22 at 16:13