1
votes

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!

4
if word in valid will not work the way you want. You need to check each character of the word individually.Thomas Weller

4 Answers

0
votes

Your 'price_to_float' function doesn't seem to be working the way you describe.

I'd recommend you that at first:

while word.lower() != "exit":
try:
    d = float(word)
    print(d)
    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")
except (EOFError):
    break

Then look for how to work with regular expressions with python 3: https://docs.python.org/2/library/re.html

0
votes

There are two issues.

  1. Your validity check

    if word in valid:
    

    will check if the whole number can be found in the list of valid characters.

    A solution would be

    if all(character in valid for character in word):
    

    Or, being a bit more explicit:

    for ch in word:
      if not ch in valid:
        return -1
    return float(word.replace("$",""))
    
  2. The dollar sign and comma

    The dollar sign and comma prevents conversion to float, because it's unexpected.

    Remove it before converting to float:

    return float(word.replace("$","").replace(",",""))
    
0
votes

look at this simple example, it shows what the misunderstanding in your code:

>>>"1.23" in ".123"
False

=> stringA in stringB returns only true, if stringA as a whole is included in StringB. It does not check each individual letter by default.

To control your input using try&except is a common method:

#python3
while True:

    word = input("Enter Amount ($X,XXX.XX):\n")

    if word.lower() == "exit": break

    word = word.replace(",","") #remove comma
    word = word.replace("$","") #remove dollar sign

    try:
        d = float(word)
    except ValueError:
        continue

    tax = 0.06
    print("Amount:",round(d,2))
    print("Tax:", (tax * d))
    print("Price w/ Tax:",round(d+d*tax,2))
-1
votes

There is a problem with your price_to_float function:

...
if word in valid:
...

This looks for an exact duplicate of the entire string word in the string valid, which will never be true if word starts with a dollar sign followed by a number.

This answer to a similar question should do what you need: https://stackoverflow.com/a/8422055/4287278