0

It may be simple, but I have only been doing this a week.

I am learning to define functions, so I was doing Columbus, Ohio tax as a test.

I keep getting a space between the dollar amount and the total no matter what I try. I was hoping someone had a solution. Again I am very new and just here to learn.

>>> def tax_ohio(subtotal):
        '''(number) -> number

Gives the total after Ohio tax given the
cost of an item.

>>> tax_ohio(100)
$107.5
>>> tax_ohio(50)
$53.75
'''
total = round(subtotal*1.075, 2)
return print('$',total)

>>> tax_ohio(100)
$ 107.5
2
  • A sidenote: I'd rather use decimal for financial data. Commented Apr 15, 2015 at 4:53
  • A small point here - the results of your execution will always have 2 decimal places, so the answer will be $107.50 and not $107.5. Commented Apr 15, 2015 at 4:54

4 Answers 4

3

Use + instead of comma in the print function., in the print function would print the default sep value ie, space.

print('$'+str(total))
Sign up to request clarification or add additional context in comments.

3 Comments

>>> tax_ohio(100) Traceback (most recent call last): File "<pyshell#102>", line 1, in <module> tax_ohio(100) File "<pyshell#101>", line 13, in tax_ohio return print('$'+total) TypeError: Can't convert 'float' object to str implicitly >>> I get this error when I attempt that.
check my edit..it's str(total) . You could do like this print('$',str(total), sep="") also.
Thanks. Super useful to be able to convert numbers to strings.
3

Use string formatting:

print('${}'.format(total))

3 Comments

Perfect, thanks! I will be able to use that in the future as well!
It might worth considering '${:.2f}'.format(total).
Good point @bereal, but considering there is already total = round(subtotal*1.075, 2) the number is already at the right precision.
1

For avoiding the space, concatenate the variables with the + operator:

def tax_ohio(subtotal):
   total = round(subtotal*1.075, 2)
   print '$'+str(total)

The , automatically appends a space between the variables.

PS. Note that you have to manually cast the float to string, otherwise you would receive the following error:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Comments

0

Because you are using print with multiple arguments, which automatically puts spaces in between. Instead use string concatenation. use $+str(total) instead.

The str() function converts the number to a string

and the + operator concatenates (joins) the two gives strings.

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.