1
def main():
    M = float(input('Please enter sales for Monday: '))
    T = float(input('Please enter sales for Tuesday: '))
    W = float(input('Please enter sales for Wednesday: '))
    R = float(input('Please enter sales for Thursday: '))
    F = float(input('Please enter sales for Friday: '))
    sales = [M, T, W, R, F]

        total = 0 

    for value in sales:
        total += value

    print ('The total sales for the week are: $',total('.2f'))

main()

With the .2f format I am getting this exception:

TypeError: 'float' object is not callable

If I remove the .2f the script runs properly but the formatting is not how I would like it, it displays as:

The total sales for the week are: $ 2500.0

I would prefer to have it so that there are 2 decimal places and no space between the $ sign.

Still new to python and learning the basics. Your help is greatly appreciated.

1

7 Answers 7

5

replace

print ('The total sales for the week are: $',total('.2f'))

for

print ('The total sales for the week are: $',"{0:.2f}".format(total))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Used your format statement and removed the comma and the format came out exactly how I was looking for. It now reads: The total sales for the week are: $2500.00
3

In python, you can format strings in a variety of ways. Here are some good resources on them:

For your case, you can format the total value like this:

>>> total = 1234.56789

>>> "{:.2f}".format(total)
'1234.57'

>>> "%.2f" % total
'1234.57'

# This only works in 3.7 and later
>>> f"{total:.2f}"
'1234.57'

For your particular case, you can format the entire print string in one go:

print(f"The total sales for the week are: ${total:.2f}")

1 Comment

@MadPhysicist Thank you, I can't believe I left it out!
2

here's a solution using python's 3 f-string feature

print (f'The total sales for the week are: {total:.2f}')

Comments

1

You may total a list with the builtin sum function. Try print(sum(sales)).

You may format your floating point like this print(f'Week is {sum(sales):.2f}')

Small nits.

Keep Hacking! Keep notes.

Comments

0

check here Limiting floats to two decimal points

sample code

def main():
    M = float(input('Please enter sales for Monday: '))
    T = float(input('Please enter sales for Tuesday: '))
    W = float(input('Please enter sales for Wednesday: '))
    R = float(input('Please enter sales for Thursday: '))
    F = float(input('Please enter sales for Friday: '))
    sales = [M, T, W, R, F]

    total = 0 

    for value in sales:
        total += value
    total = "{0:.2f}".format(total)
    print ('The total sales for the week are: $' + str(total))

main()

Comments

0

Formatters in Python allow you to use curly braces as placeholders for values that you’ll pass through with the str.format() method.

As per your requirement

total = 0 
print ('The total sales for the week are: $',"{0:.2f}".format(total))

Comments

0

Just make this correction:

print('The total sales for the week are:',format(total,('.2f')),'$')

Comments

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.