Continued invalid syntax for print function. Please help

I have tried the following code with both parenthesis and without and I continue to get a syntax error for invalid syntax. Please help. I am using python 3.10.2.
EC124601-CD24-4507-9B15-AC8B7F7BE8C1_4_5005_c

Please don’t show screenshots or images of code, as we don’t write code using Photoshop. You force us to retype your code, which may introduce errors, and is annoying to everyone.

Copy and paste the text of the code.

In your case, you need to put parentheses around the argument going to print:

# Wrong.
print"| Base 10 | Base %2 |"%(int(sys.argv[1]))

To fix the syntax error you need this:

# Better, but still wrong.
print("| Base 10 | Base %2 |" % int(sys.argv[1]))

If you try that, you will get an error like this (or similar):

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unsupported format character ' ' (0x20) at index 19

The problem here is that you are asking Python to take the value:

int(sys.argv[1])

and insert it into the string at the position marked with a %, but that position is marked wrongly.

You have the position marked using "%2 " (note the space at the end) but that doesn’t mean anything to the intepreter. The space is invalid, it needs to be a format character like “d”, “f”, or a few others.

My guess is that what you want is:

# Maybe what you want?
print("| Base 10 | Base %2d |" % int(sys.argv[1]))

The “d” format code tells the interpreter to expect to be supplied an int value, and insert it into the string using at least two characters.

1 Like

These still all give the error as well, here is my written code sorry for the image:
import sys
#method to convert base
def convert(number, base):
result = “0.”
while number!=0:
prefix = “”
#if width of base is larger
if len(str(base)) > len(str(int(numberbase))):
prefix = “0”
(len(str(base)) - len(str(int(numberbase))))
#appending the ineger part of base
number
result += prefix + str(int(numberbase))
#retrieving the remaining fractional part
number= number
base - int(number*base)
return float(result)
#displaying formatted output
print("| Base 10 | Base %2d |" % int(sys.argv[1]))
SyntaxError: invalid syntax

I think I have worked out the error.

Your code is fine, if you save it to a .py file and then run it, it works fine. So there’s not actually anything wrong with your code now.

But you are using IDLE, yes?

The IDLE system has, I think, some limitations compared to the regular Python interpreter. Specifically, it sometimes needs an extra blank line otherwise it gets confused and gives an incorrect SyntaxError.

Try this:

After the end of the convert() function, insert a blank line between the return and the next line, like this:

# Currently the file ends like this:

    return float(result)
#displaying formatted output
print("| Base 10 | Base %2d |" % int(sys.argv[1]))

Insert a blank line after the return float(result) line, so it looks like this:

    return float(result)

then a blank line, then

#displaying formatted output
print("| Base 10 | Base %2d |" % int(sys.argv[1]))

In my testing, that fixes the problem.

The interactive interpreter built into Python has a similar issue, but it is not as bad as the IDLE interpreter. So I have raised two bug reports for this issue:

Please don’t delete your post(s) as the bug reports link back to this discussion.

1 Like

My goodness…

(It’s a great practice to begin a reply with “Thank you”, especially when someone has put in as much effort as Steven did. The work put into formatting alone deserves some appreciation.)

("Conversely, it’s also an excellent idea to not say, in effect, “your experienced and freely volunteered help didn’t work and wasn’t helpful.”)

Speaking of formatting, please always format your code by fencing it with three backticks (grave accent mark). Use ```python as the opening line and it will make your pasted code look nice and readable, like this:

if goodManners and goodPractices then:
    print("Nice Work!")

Guidance on best post formatting practices is available HERE.

And we all thank YOU, Ron, for posting this issue for Steven to find and follow up with the two bug reports.

Arigato!

Thank you so much!