-3

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
  • 15
  • 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,436
  • 2
  • 35
  • 43
1

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__":

LMD
  • 2,802
  • 1
  • 11
  • 24
  • 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 – LMD 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 ? – LMD 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
-1

It is likely due to a mismatched paranthesis prior to the "if" statement. Check if all your "("s are matched by a closing ")".

Kosa
  • 37
  • 5
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 28 '21 at 00:22
  • 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