Python Dict 2 参数,如何用一个退出 while 循环

发布于 2025-01-12 01:59:24 字数 714 浏览 0 评论 0原文

已经编写了下面的程序,但是如何避免系统在按下 Enter 时检查两个参数,我们使用 Enter 键退出 while 循环。

while True: # need to exit the while loop thru one <Enter>
    

    itemcode, quantity = input("Enter item code and quantity (Press <Enter> key to end): ") .split()


    if itemcode == " " : #got two argument, one enter will crash the system, how to amend it
        return
    
    if itemcode not in orderdict.keys():
        print("Invalid item code! Try again.")
        continue

    if itemcode in orderdict.keys():
        orderdict [itemcode] = orderdict 
        #print("{:4s} {:<3d} {:<3d} X {:<3d} ".format(itemcode, [0], [1], [3]))    
    
    else :
        input ()   
        break

Have written below program, but how to avoid the system checking the two argument when enter is pressed, we are using the enter key to exit the while loop.

while True: # need to exit the while loop thru one <Enter>
    

    itemcode, quantity = input("Enter item code and quantity (Press <Enter> key to end): ") .split()


    if itemcode == " " : #got two argument, one enter will crash the system, how to amend it
        return
    
    if itemcode not in orderdict.keys():
        print("Invalid item code! Try again.")
        continue

    if itemcode in orderdict.keys():
        orderdict [itemcode] = orderdict 
        #print("{:4s} {:<3d} {:<3d} X {:<3d} ".format(itemcode, [0], [1], [3]))    
    
    else :
        input ()   
        break

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贵在坚持 2025-01-19 01:59:24

您的问题是,第一行是硬编码的,程序需要两个且恰好两个由空格分隔的值。如果您给它的值少于两个(并且简单地按“回车”意味着给它零值),或超过两个,这会导致回溯,因为预期的值数量不存在。

如果您不信任用户输入正确数量的值,那么我认为您可以使用 try/exception 块或重复提示用户输入的简单循环来避免崩溃直到输入的值的数量正好是 2:

while True:
    entry_list = input("Enter item code and quantity (Press <Enter> key to end): ") .split()
    if len(entry_list) == 2:
        itemcode = entry_list[0]
        quantity = entry_list[1]
        break

Your problem is that the first line is hardcoded in such a way that the program expects two, and exactly two, values separated by a space. If you give it less than two (and simply hitting "enter" means giving it zero values), or more than two, this causes a traceback because the expected number of values isn't there.

If you don'T trust your users to enter the correct number of values, then I think you can avoid a crash either with a try/exception block, or a simple loop that repeats promtping the user for input until the number of entered values is exactly two:

while True:
    entry_list = input("Enter item code and quantity (Press <Enter> key to end): ") .split()
    if len(entry_list) == 2:
        itemcode = entry_list[0]
        quantity = entry_list[1]
        break
转角预定愛 2025-01-19 01:59:24

您可以开始要求用户仅输入该项目的值,并检查该值是否为回车键,然后中断循环。否则,检查该项目的值是否在键内(如果是),然后询问用户相应的数量。我认为你需要替换 orderdict
以及用户输入的商品相应的数量

orderdict = {"car":50,"plane":60,"bus":100} # example

while True: 

    itemcode = input("Enter item code and quantity (Press <Enter> key to end): ")

    if itemcode == "" :
        break
    elif itemcode not in orderdict:
        print("Invalid item code! Try again.")
    else:
        orderdict [itemcode] = input("Enter the quantity: ") 

注意:不需要编写orderdict.keys(),只使用字典的名称即可迭代orderdict的键>。

You can start to ask the user to enter only the value of the item and check this value if it is an enter key, then break the loop. Otherwise, check the value of the item if it is within the keys if so, then ask the user for the corresponding quantity. I think you need to replace orderdict
with the corresponding quantity for the item entered by the user.

orderdict = {"car":50,"plane":60,"bus":100} # example

while True: 

    itemcode = input("Enter item code and quantity (Press <Enter> key to end): ")

    if itemcode == "" :
        break
    elif itemcode not in orderdict:
        print("Invalid item code! Try again.")
    else:
        orderdict [itemcode] = input("Enter the quantity: ") 

Note: you do not need to write orderdict.keys(), you can use only the name of a dictionary to iterate through the keys of orderdict.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文