0

I've tried searching for possible ways to set limits for the input(). It takes only absolute numbers, and I'd like to add a range for example 0-100. Could you do it from the input() function itself or a if would be required? If out of range, I don't want any output, just a error. Thanks in advance

price_flour_kilo = abs(float(input()))
kilos_flour = abs(float(input()))
kilos_sugar = abs(float(input()))
packs_eggs = abs(float(input()))
packs_yeast = abs(float(input()))
price_sugar_kilo = price_flour_kilo * 0.75
price_pack_eggs = price_flour_kilo * 1.1
price_yeast_pack = price_sugar_kilo * 0.2
total_flour = price_flour_kilo * kilos_flour
total_sugar = price_sugar_kilo * kilos_sugar
total_eggs = price_pack_eggs * packs_eggs
total_yeast = price_yeast_pack * packs_yeast
result = total_eggs + total_flour + total_sugar + total_yeast
print(round(result, 2))
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119

2 Answers2

0

You can define your input values by yourself:

def get_input(message, desired_values):
    user_input = input(message)
    if int(user_input) in desired_values:
        return user_input
    else:
        return get_input("Wrong input, possible inputs: {}: ".format(str(desired_values)), desired_values)

inp = get_input("Please your number: ", range(100))
Raymond Reddington
  • 1,709
  • 1
  • 13
  • 21
0

The input() function only reads an input. Even when you get an error when you enter a non-integer value to int(input()), it's the int() function that gives you an error. So it can't be done through the input() function. Any input handling of the kind that you're talking about has to be done using if-else conditions or something of the sort. Check whether the input satisfies your conditions and throw an error, if not.

magikarp
  • 460
  • 1
  • 8
  • 22