-2

Hi I want to have my display to be for example, Single room 4 $720.00 But I do not know how to have the dollar sign there without having the error that it doesn't go with a float value.

single_room = int(input("Number of Single room: "))
total_single = 90 * single_room
print(f'{"Single room":<12}{single_room:^3}{"$" + total_single:>9.2f}')

This is an error because string doesn't go along with the float value. Please assist

Cruz
  • 1
  • 1
    You need the dollar sign to be as part of the string itself, not as part of a variable... `f'{"Single room":<12}{single_room:^3}${total_single:>9.2f}'` – Tomerikoo Apr 13 '21 at 19:20
  • Umm, sorry if this is a dumb question, but - I don't understand your print. Can you add an explanation of what the print line is supposed to do ? – TheEagle Apr 13 '21 at 19:21
  • @Programmer It says in the question, it's supposed to print `Single room 4 $720.00` – Tomerikoo Apr 13 '21 at 19:22
  • @Tomerikoo Hi, I tried to add the dollar sign there but it doesn't align to the right hand side – Cruz Apr 13 '21 at 19:22
  • 1
    Check out [Currency formatting in Python](https://stackoverflow.com/q/320929/10077). – Fred Larson Apr 13 '21 at 19:49
  • @Tomerikoo I know this, but I wanted to understand his code (`f'{"Single room":<12}{single_room:^3}{"$" + total_single:>9.2f}'`) ... – TheEagle Apr 13 '21 at 20:04

2 Answers2

1

so you're f-string interpolating:

print(f"${total_single}") 

should work, try and adapt it to your problem. The $ is not part of the variable

WiseStrawberry
  • 317
  • 1
  • 4
  • 14
0

If you want the price to be aligned to the right together with the $ sign, you can format in two steps:

single_room = int(input("Number of Single room: "))
total_single = f"${90 * single_room:.2f}"
print(f'Single room {single_room:^3}{total_single:>9}')

Example run:

Number of Single room: 4
Single room  4   $360.00

First, format the $ together with the price as a string into total_single. Then, align it as you want in the final string.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61