Print invalid syntax

Hi, I’m doing a HackerRank exercise, and I keep getting an invalidSyntax with the print command but it won’t explain why it’s invalid:

File “main.py”, line 9
print(i) #testing the print function
^
SyntaxError: invalid syntax

Here is my code. There are parts in hashtag that are my previous attempts that I’ve left there in case they can be used again, but don’t mind it. Because this is a HackerRank exercise, the part of the code that I labeled at the end cannot be edited, but I don’t think this has to do with the invalid syntax…

def print_formatted(number):
    for i in range(number):
        # other code (don't mind this):
            # print(deci(i))
            # print(octa(i))
            # print(hexa(i))
            # print(bi(i))
        printer = (" ", str(deci(i)), " ", str(octa(i)), " ", str(hexa(i)), " ", str(bi(i))
        print(i) #testing the print function
        print("    ".join(printer))
    
def deci(a):
    return(float(a))
def octa(b):
    return(oct(b))
def hexa(c):
    return(hex(c))
def bi(d):
    return(bin(d))  

# permanent given code
if __name__ == '__main__':
    n = int(input())
    print_formatted(n)

What happens when you remove the code that gave you the syntax error?

When I removed both the print lines below “printer” I got:
File “main.py”, line 11
def deci(a):
^
SyntaxError: invalid syntax

P.S. I didn’t think of checking how it would go if the print commands weren’t there. Thank you.

Also after removing the 4 def lines, I got:

File “main.py”, line 22
if name == ‘main’:
^
SyntaxError: invalid syntax

Shouldn’t the permanent section of the exercise be totally correct?

Keep going, remove that, too.

After removing the 4 def lines, I got:

File “main.py”, line 22
if name == ‘main’:
^
SyntaxError: invalid syntax

Shouldn’t the permanent section of the exercise be totally correct?

What’s the point of saying that again?

I’m sorry, the point of saying what? If you mean the fact that I replied twice it was an error, I’m sorry, I thought the first time it didn’t post.

Yes, I meant the duplicate post.

Oh, I’m very sorry then. A lot of times when I use iMessage the text looks like it’s posted but then takes a while to notify me and show the not sended red message. I don’t have much experience using other post platforms so… again, sorry.

As amusing (and educational) as it may be to do this line by line, I’ll give you a hint:

1 Like

Wow thank you, Alexander. I can’t believe I made such a stupid mistake! :joy:

I think they were almost there. I just don’t know why it’s taking them over 20 minutes to remove the last few lines, too.

General tip: If you get a syntax error and the line it’s pointing to doesn’t look wrong, move up a little. The error is often a simple one on the previous line (a forgotten close bracket of some form, a dangling operator, etc). This is true in all languages; you’ll find the same thing in JavaScript or C, too.

The easiest way to figure out if that’s the problem is what @Stefan2 initially suggested: Remove the line of code that is reporting the error. If the error instantly moves down to the next line, there’s a VERY high chance that the bug is actually a line above it.

Almost all programmers’ editors have some sort of tool for helping you match parentheses (colouring them differently, or showing the one that pairs with the one next to the cursor, or something). Get to know how your editor shows this. If the editor disagrees with your expectations about the code, that’s a really good sign that there’s a bug!

4 Likes

Yes, that was the goal/plan. Had they kept going and removed all the rest, they finally would’ve realized there must be a problem in the code above. I think that’s still a valuable exercise to do once, even if in the future your “If the error instantly moves down [then look above]” rule is what you do.

3 Likes

Stefan2, abessman, and Rosuav , thank you all for your advice! It’s been really helpful.

Yup! I figured that was your intent, but in case the OP hadn’t picked up on that, I wanted to elaborate.

Debugging is hard. Experts aren’t experts because we write bug-free code, nor because we can instantly and magically recognize issues. (Really, the only difference between an expert and a novice is the number of mistakes we’ve made. :slight_smile: ) Fortunately, Python does a GREAT job of telling us about problems; but we need to learn how to understand it. Just like Sherlock Holmes did, we can look truly astounding if we know how to read the right clues - a splash of mud for him, a strange reference in a traceback for us…

2 Likes

Oh that reminds me. In Python 3.12, I actually got this for their full original code:

  File "/ATO/code", line 8
    printer = (" ", str(deci(i)), " ", str(octa(i)), " ", str(hexa(i)), " ", str(bi(i))
              ^
SyntaxError: '(' was never closed
1 Like

@Rosuav , I love that reference! And thank you for your encouragement. I know this isn’t really related to Python, but I just wanted to say I really appreciated it. Your comments give off a very positive vibe.

1 Like

Reference: