Home > Blockchain >  How do I limit individual dictionary inputs in Python?
How do I limit individual dictionary inputs in Python?

Time:08-01

Is there a way to put limitations on the integer input of a dictionary? The current way I have it set up checks the whole dictionaries individual inputs after, is there a way I can set it up so it can give more instant feed back. E.g. User inputs an integer value of 6 then an error and prompt appears straight away?

def shop_dictionary(shop):
    shop_dict = {}
    for key in range(1, shop   1):
        shop_name = f"Shop {key}"
        while True:
            try:
                shop_dict[shop_name] = {
                    "Chocolate": int(input(f"{shop_name} Chocolate Amount: ")),
                    "Strawberry": int(input(f"{shop_name} Strawberry Amount: ")),
                    "Vanilla": int(input(f"{shop_name} Vanilla Amount: ")),
                }
                break
            except ValueError:
                print(
                    "Invalid. Please enter an amount between 1 and 5."
                )
        if shop_dict[shop_name]["Chocolate"] < 1 or shop_dict[shop_name]["Chocolate"] > 5:
            print("Invalid number. Please enter a number between 1 and 5.")
            shop_dict[shop_name]["Chocolate"] = int(input(""))
        if shop_dict[shop_name]["Strawberry"] < 1 or shop_dict[shop_name]["Strawberry"] > 5:
            print("Invalid number. Please enter a number between 1 and 5.")
            shop_dict[shop_name]["Strawberry"] = int(input(""))
        if (
            shop_dict[shop_name]["Vanilla"] < 1
            or shop_dict[shop_name]["Vanilla"] > 5
        ):
            print("Invalid number. Please enter a number between 1 and 5.")
            shop_dict[shop_name]["Vanilla"] = int(input(""))
    return shop_dict

Preferably I would like to know if this is possible during the input stage, is there something I could write that would do something like the following?

                shop_dict[shop_name] = {
                    "Chocolate": integer between range of 1 to 5(input(f"{shop_name} Chocolate Amount: ")),
                    "Strawberry": integer between range of 1 to 5(input(f"{shop_name} Strawberry Amount: ")),
                    "Vanilla": integer between range of 1 to 5(input(f"{shop_name} Vanilla Amount: ")),
                }

Thanks. Sorry for the noob question.

CodePudding user response:

Create a helper function that doesn't return until user enters correct value.

def get_int(prompt, minn, maxx):
    while True:
        try:
            val = int(input(prompt))
            if minn <= val <= maxx:
                return val
        except:
            pass
        print(f"Invalid number. Please enter a number between {minn} and {maxx}.")

def shop_dictionary(shop):
    flavors = ["Chocolate", "Strawberry", "Vanilla"]
    shop_dict = {}
    for key in range(1, shop   1):
        shop_name = f"Shop {key}"
        shop_dict[shop_name] = {flavor: get_int(f"{shop_name} {flavor} Amount: ",  1, 5) for flavor in flavors}
    return shop_dict

If you are not familiar with dictionary comprehension, the line

shop_dict[shop_name] = {flavor: get_int(f"{shop_name} {flavor} Amount: ",  1, 5) for flavor in flavors}

is the same as

shop_dict[shop_name] = {}
for flavor in flavors:
    shop_dict[shop_name][flavor] = get_int(f"{shop_name} {flavor} Amount: ",  1, 5) 
  • Related