-1

I want to make a function with multiple Arguments and Can only be from A Dictionary. I tried Using these Codes:

Milk = "1L"
Water = "10L"
Juice = "500ml"

def Quantity(*Liquid):

    AllLiquids = { Milk: "Milk", Water: "Water", Juice: "Juice" }
    
    if Liquid in AllLiquids:
        for i in Liquid:
            print(i)
    else:
        print("An Error Occured")

Quantity(Water, Milk)

Instead of Getting the Quantity I Get this Output (Which is in the Else Statement):

An Error Occured

How Can I Do This Correctly?

Sam1009
  • 3
  • 3
  • `*Liquid` means that `Liquid` is a `tuple`. Drop the asterisk or iterate over `Liquid` – C.Nivs Aug 24 '23 at 14:50
  • Does this answer your question? [Can a variable number of arguments be passed to a function?](https://stackoverflow.com/questions/919680/can-a-variable-number-of-arguments-be-passed-to-a-function) – Solomon Slow Aug 24 '23 at 15:02

1 Answers1

0

The overall intention of your code is unclear. It seems very odd to pass the quantities as arguments to the Quantity function and then want these printed back. At a guess, perhaps you are intending something like this:

Milk = "1L"
Water = "10L"
Juice = "500ml"

def Quantity(*Liquid):

    AllLiquids = { "Milk": Milk, "Water": Water, "Juice": Juice}
    
    for drink in Liquid:
        if drink in AllLiquids:
            print(drink, AllLiquids[drink])
        else:
            print("An Error Occured")

Quantity("Water", "Milk")

which gives:

Water 10L
Milk 1L
user19077881
  • 3,643
  • 2
  • 3
  • 14