Home > Enterprise >  Trying to input a blank for an input that requires 2 values separated by ", "
Trying to input a blank for an input that requires 2 values separated by ", "

Time:11-27

So I am trying to have the while loop end when inputting a blank for the input, but the problem is that the input takes 2 values separated by ", ". It is necessary for me to keep the input like that rather than separating them so how to fix this?

print(" Input the productIDs and quantities (input blank to complete transaction)")
    productID, quantity = input().split(", ")
    quantity = int(quantity)
    while quantity >= 1:
        self.addProductToTransaction(productID, quantity) 
    print("why u here bro u ain't buyin nothin")

When input is blank:

ValueError: not enough values to unpack (expected 2, got 1)

CodePudding user response:

You don't even need a execption handelling. Simply take as a string then split by '

print(" Input the productIDs and quantities (input blank to complete transaction)")
user_in = input()
if user_in !='':

    productID, quantity = user_in.split(',')
    print(quantity)
    quantity = int(quantity)
    while quantity >= 1:
        self.addProductToTransaction(productID, quantity) 
   
else:
    
    print("why u here bro u ain't buyin nothin")

Sample outs#

 Input the productIDs and quantities (input blank to complete transaction)

why u here bro u ain't buyin nothin

CodePudding user response:

while loop should be outer, if you want to iteratively receive the input until a bad format is fed (handled by try-except).

while True:
    try:
        productID, quantity = input("Input the productIDs and quantities (input blank to complete transaction)").split(", ")
        quantity = int(quantity)
    except ValueError:
        print("why u here bro u ain't buyin nothin")
        break
    if quantity >= 1:
        self.addProductToTransaction(productID, quantity) 

CodePudding user response:

There are three ways to do this. Using len() or with try..except or with if

if

while True:

    var = input("Input the productIDs and quantities (input blank to complete transaction)")
   
    if len(var) == ‘’: 

        print("why u here bro u ain't buyin nothin")
        break

    productID, quantity = Var.split(", ")



    quantity = int(quantity)
    if quantity >= 1:
        self.addProductToTransaction(productID, quantity) 

len()


while True:

    var = input("Input the productIDs and quantities (input blank to complete transaction)")
   
    if len(var) == 0: 

        print("why u here bro u ain't buyin nothin")
        break

    productID, quantity = Var.split(", ")



    quantity = int(quantity)
    if quantity >= 1:
        self.addProductToTransaction(productID, quantity) 

try…except


while True:

    try:


        productID, quantity = input("Input the productIDs and quantities (input blank to complete transaction)").split(", ")

        quantity = int(quantity)
        if quantity >= 1:
            self.addProductToTransaction(productID, quantity) 


    except ValueError:
    print("why u here bro u ain't buyin nothin")
    break

Three notes:

  1. If you are printing a message for input the pass the string in the input function. And if you want user to input on next line, pass new line escape chat in the string "Foo..string\n"
  2. I think
  • Related