0

I am trying to have my program format properly in a tkinter GUI but for some reason I get the error message:

    product += ('%-10s%-10s%-0s%-0s') % (str(names[i])+ str(addedHours[i]) + str(payOut) + "\n") TypeError: not enough arguments for format string

Desired Output: enter image description here

Here is the snip of code related to the problem (Note that the Name/Hours/Pay and --- parts are working fine, just not the ones under the variable product)

    def printPayroll(self):

        i = 0
        product = ""
        for y in names:

            payOut = float(wage[i]) * float(addedHours[i])
            product += ('%-10s%-10s%-0s%-0s') % (str(names[i])+ str(addedHours[i]) + str(payOut) + "\n")

            i += 1

        self.text.insert(END,("%-10s%-10s%-0s") % ('Name', 'Hours', 'Pay\n'))
        self.text.insert(END,("%-10s%-10s%-0s") % ('---','-----','---\n'))

        self.text.insert(END, product)

1 Answer 1

1

The error message you get, TypeError: not enough arguments for format string, is telling you exactly what the problem is.

Consider this line of code:

product += ('%-10s%-10s%-0s%-0s') % (str(names[i])+ str(addedHours[i]) + str(payOut) + "\n") 

The above code is functionally identical to this:

s = str(names[i]) + str(addedHours[i]) + str(payOut) + "\n"
product += ('%-10s%-10s%-0s%-0s') % s

Your formatting string requires four arguments but you are only giving one. The simple solution is to replace every + with a ,:

product += ('%-10s%-10s%-0s%-0s') % (str(names[i]), str(addedHours[i]),  str(payOut), "\n")
Sign up to request clarification or add additional context in comments.

5 Comments

what a god <3 <3 <3
@jimmyjohn123: no, not really. You just need to read the error message and assume it's telling you the truth. Then, ask yourself "why does it think I'm only providing one argument?".
also, if I were to insert a dollar sign in the str(payOut) what would I need to do?
yeah that is true, I assumed using '+' separates then into different arguments like how ',' does it.
@jimmyjohn123: if you want a dollar sign in the output, you would put it in the format string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.