python 尝试为需要用“,“分隔的2个值的输入输入空白

ycl3bljg  于 2022-11-28  发布在  Python
关注(0)|答案(4)|浏览(162)

因此,我试图在输入空白时结束while循环,但问题是输入采用了2个由","分隔的值。对我来说,有必要保持输入,而不是将它们分隔开,那么如何解决这个问题?

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")

输入为空时:

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

cgfeq70w1#

你甚至不需要一个异常处理。简单地取一个字符串,然后按'拆分

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")

抽样数量

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

why u here bro u ain't buyin nothin
92vpleto

92vpleto2#

while loop应该是外部的,如果你想迭代地接收输入,直到输入一个错误的格式(由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)
g2ieeal7

g2ieeal73#

有三种方法可以做到这一点。使用len()try..exceptif
假如

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)

镜头()

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)

尝试...除了

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

三个注意事项:
1.如果要打印输入的消息,请在输入函数中传递字符串。如果要用户在下一行输入,请在字符串"Foo..string\n"中传递新行转义字符
1.我想🤔你是想让循环反复询问用户输入,然后将其传递给另一个类函数addProductToTransaction()。除非没有传递任何内容,否则退出循环。为此,你可以将循环放在输入之前。
1.以上解决方案按首选项和性能(从高到低)排序

iqjalb3h

iqjalb3h4#

我设法使用try/except修复了它。

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

相关问题