-1

I'm running into an issue with my Python program.

In fact, I have a sales price input which can contain a dollar sign and must respect the following constraints :

  1. It must be numeric
  2. The sales price must be at least $100, including dollars and cents

The problem I'm facing is : if the user inputs a sales price including a dollar sign, how to "ignore" it in order to determine if the input is numeric ? The same issue applies when it comes to a decimal point.

Also, note that :

  • If the input is not a numeric entry, I would like it to prompt "price must be numeric".
  • If the entry is less than $100, I would like it to prompt "Price must be at least $100".

This is what I have so far :

salesPrice= input("What is the sales price of the new phone? ")

if salesPrice[0]=="$":
    val= int(salesPrice[1:])
    if val < 100
        print('Price must be at least $100')
    else:
        price=float(salesPrice[1:])
elif ValueError:
    print('Price must be numeric')
else:
    if salesPrice[0]!= '$':
        if salesPrice[0:]< Min_price:
        print('Price must be at least $100')
        else:
            price=float(salesPrice[0:])
Hamza Zubair
  • 1,232
  • 13
  • 21
t_J
  • 21
  • 3

4 Answers4

0

I'm assuming you're using Python3 since your use of input() doesn't throw a SyntaxError upon receiving a non-numeric string. Either way, only the input should change. For Python3:

salesPrice = input("What is the sales price of the new phone? ").replace('$', '')
try:
    salesPrice = float(salesPrice)
    if salesPrice < 100.:
        print("Price must be at least $100")
except ValueError:
    print("Price must be numeric")

And for Python 2:

salesPrice = raw_input("What is the sales price of the new phone? ").replace('$', '')
try:
    salesPrice = float(salesPrice)
    if salesPrice < 100.:
        print("Price must be at least $100")
except ValueError:
    print("Price must be numeric")

For both, you can check if a string is a float with a simple try/except.

Jack Lynch
  • 155
  • 2
  • 10
0

I would separate two concerns: dealing $ sign and number logic, separately.


salesPrice= input("What is the sales price of the new phone? ")

# remove the optional $ sign
if salesPrice[0]=="$":
    salesPrice = salesPrice[1:]

# your number logic
...

willhyper
  • 96
  • 2
  • 5
0

This works fine:

salesPrice= input("What is the sales price of the new phone? ")

if salesPrice.startswith("$"):

    amount = salesPrice.replace("$", "").strip()
    if amount.isnumeric():
        val = int(amount)
        if val < 100:
            print('Price must be at least $100')
        else:
            price=float(val)
    else:
        print('Price must be numeric')
else:
    if salesPrice.isnumeric():
        salesPrice = int(salesPrice)
        if salesPrice < 100:
            print('Price must be at least $100')
        else:
            price=float(salesPrice)
Priyank-py
  • 349
  • 3
  • 14
0

Use the following if you would like to handle any currency and not just dollars.

The regular expression code extracts the numbers and checks if it is less than 100 or not.

import re

input_price = input("What is the sales price of the new phone? ")
lst_find = re.findall('(\d+)', input_price)
if len(lst_find) != 0:
    price = int(lst_find[0])
    if price < 100:
        print('Price must be atleast $100')
else:
    print('Price must be numeric')
Hamza Zubair
  • 1,232
  • 13
  • 21