I have a code that asks you to input an amount of money. The user can input numbers from 0-9, commas ",", decimals ".", and dollar signs "$". So the program should be able to read an input such as "$4,599.80 and know it means "4599.80". The program will also determine the tax for the amount of money you inputted. The tax is 6% so the program will take your input and multiply that by 0.06 and output what the tax of the amount of money is. Then, the program should output the TOTAL cost of the amount of money you inputted WITH the tax as well. So if you inputted "$100", the program should output that the tax is $6 and the total amount is $106. If the user enters something else besides an amount such as any other symbols (besides the ones I listed above), or words, the program is supposed to output "-1" and say the tax will be "-0.06" and the total is "-1.06". The program should continue to ask the user to input an amount until the user inputs "exit". My program keeps saying the output is "-1" even if you enter an eligible number.
Here is an input:
100.00
200
98.78
$1,009.78
Goat
exit
Here is what the output SHOULD be
Determine Price with Tax.
Enter 'exit' at any time to quit.
Enter Amount ($X,XXX.XX):
Amount: 100.0
Tax: 6.0
Price w/ Tax: 106.0
Enter Amount ($X,XXX.XX):
Amount: 200
Tax: 12.0
Price w/ Tax: 212.0
Enter Amount ($X,XXX.XX):
Amount: 98.78
Tax: 5.93
Price w/ Tax: 104.71
Enter Amount ($X,XXX.XX):
Amount: 1009.78
Tax: 60.59
Price w/ Tax: 1070.37
Enter Amount ($X,XXX.XX):
Amount: -1
Tax: -0.06
Price w/ Tax: -1.06
Enter Amount ($X,XXX.XX):
Here is MY output
Determine Price with Tax.
Enter 'exit' at any time to quit.
Enter Amount ($X,XXX.XX):
Amount: -1
Tax: -0.06
Price w/ Tax: -1.06
Enter Amount ($X,XXX.XX):
Amount: -1
Tax: -0.06
Price w/ Tax: -1.06
Enter Amount ($X,XXX.XX):
Amount: -1
Tax: -0.06
Price w/ Tax: -1.06
Enter Amount ($X,XXX.XX):
Amount: -1
Tax: -0.06
Price w/ Tax: -1.06
Enter Amount ($X,XXX.XX):
Amount: -1
Tax: -0.06
Price w/ Tax: -1.06
Enter Amount ($X,XXX.XX):
Here is my code
print("Determine Price with Tax.")
print("Enter 'exit' at any time to quit.")
word = input("Enter Amount ($X,XXX.XX):\n")
def price_to_float(word):
valid = "$,.1234567890"
if word in valid:
return float(word)
else:
return -1
while word.lower() != "exit":
d = price_to_float(word)
tax = 0.06
print("Amount:",round(d,2))
print("Tax:", (tax * d))
print("Price w/ Tax:",round(d+d*tax,2))
word = input("Enter Amount ($X,XXX.XX):\n")
I think there is something wrong with my function so it might not be properly converting strings to float numbers. I would appreciate any suggestions!
if word in valid
will not work the way you want. You need to check each character of the word individually. – Thomas Weller