Skip to content
Advertisement

Input line that accepts one and two variables without breaking?

I’m trying to create a “Inventory system” as a beginner proyect. I have a dictionary with string keys and integers values, and I want the program to ask the user what materials he wants to take, and how much of it. That question takes from the materials dictionary and adds it to the “inventory” of the user. I also managed to repeat the question so that the user can take from the materials dictionary as many times as he wants.

trading_bill = True

n, q = input("What do you want to add? ").split()

while trading_bill:
    q = int(q)
    y = int(materials_dict.get(n))

    materials_dict.update({n: y - q})
    inv.update({n: q})
    print("Added.")

    n, q = input("+ ").split()

Now, I want the user to be able to write “Done” and have the program print out his inventory and quit, but the input line needs two values, so it breaks when you give it only one. Is there a way for that last input to accept only one value instead of two?

     n, q = input("+ ").split()
ValueError: not enough values to unpack (expected 2, got 1)

Advertisement

Answer

Split later.

reply = input("+ ")
if reply == 'Done':
    break
n, q = reply.split()
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement