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)
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.
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!
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.
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. ) 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…
@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.