0
cheese = float(input("Enter cheese order weight (numeric value): "))

if cheese >= 100:
    print(cheese,"is more than currently available stock ")
elif cheese <= 0.15:
    print(cheese,"is below minimum order amount")
elif cheese >= 0.16 <= 99:
    print(cheese," costs $",cheese * 7.99,sep = "")
else:
    print(cheese,"Is invlide, please try agin")

Every time I add a letter I get an error. I want to be able to add float nub and words, so I can use the else statement.

BenT
  • 3,172
  • 3
  • 18
  • 38
  • What error do you get? – Josh Clark Apr 20 '20 at 00:32
  • 1
    Question has nothing to do with `machine-learning` - kindly do not spam irrelevant tags (removed). – desertnaut Apr 20 '20 at 00:34
  • 1
    Your code has many issues - for example, what about amounts like 0.155 or 99.5 cheese? Apart from that, if you enter anything that's not a valid `float`, your first line will fail. If only there was a way to `try` it and use it `except` when there's a problem... – Grismar Apr 20 '20 at 00:40

1 Answers1

0

I'm not sure what "nub" means? But you need to put this in a loop and handle the errors using try and except:

cheese_weight = None
# Repeat until there is a valid value for cheese_weight
while not cheese_weight:
    # Step 1: get some user input
    raw_input = input("Enter cheese order weight (numeric value): ")
    # Step 2: try convert this input to a float
    try:
        cheese_weight = float(raw_input)
    # Step 3: handle the error if there is one
    except ValueError as err:
        print("The cheese weight must be a numeric value.)

note that if float(raw_input) raises an error, cheese_weight won't get a new value and so the loop will run again. If the value does parse as a float, then cheese_weight will have a value that isn't None and so the loop will end.

Dan
  • 45,079
  • 17
  • 88
  • 157