0
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.

damon
  • 14,485
  • 14
  • 56
  • 75
Christian R
  • 293
  • 2
  • 10

7 Answers7

4

replace

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

for

print ('The total sales for the week are: $',"{0:.2f}".format(total))
  • 1
    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 – Christian R Nov 22 '19 at 18:43
2

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

print (f'The total sales for the week are: {total:.2f}')
Aviad Rozenhek
  • 2,259
  • 3
  • 21
  • 42
2

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}")
damon
  • 14,485
  • 14
  • 56
  • 75
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.

Charles Merriam
  • 19,908
  • 6
  • 73
  • 83
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()
Yukun Li
  • 244
  • 1
  • 6
0

Just make this correction:

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

Ellis
  • 568
  • 8
  • 29
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))
user9634982
  • 565
  • 5
  • 24