Dan Todorovic' Dan Todorovic' - 11 months ago 49
Python Question

defining dollar and being allowed to make that a float in python

Hey sorry if the title was misleading but for my assignment for the week I must make a program that converts dollars to pounds which is easy however I cannot seem to figure out how to input a dollar sign with my code then do addition with it since it a float cannot have dollar signs in it.

I need my inputs to look like this:

Enter a dollar amount: $5.44

Enter another dollar amount: $6.67

However I cannot seem to figure out how to get the dollar sign in with the input, and then remove the dollar sign to do the adding of the two dollars.

#giving variables for the input of dollar amount
dollar1 = float(input("Enter a dollar amount: "))
dollar2 = float(input("Enter another dollar amount: "))
dollarTotal = (dollar1 + dollar2)

print("Your total is: ","$", format (dollarTotal, "0.2f"), sep="")

Everything works out fine however I can not put in dollar signs into the input and I have to to receive full credit, thank you for your help.

Answer Source

You could use s = s.lstrip('$') to remove leading dollar signs from the input string s.

To add the dollar sign to the output, just put it inside the format string:

print("Your total is: ${:,.2f}".format(dollars_total))