简体   繁体   English

我正在尝试在我的 class 中创建一个用户输入字典以获得额外的信用,但我无法弄清楚如何正确结束程序

[英]I'm trying to make a user input dictionary for extra credit in my class and I can't figure out how to properly end the program

my_dict = dict()
more = True
while more:
  bad_input = True

  while bad_input:
    user_input = input("Enter key and value seperated buy commas(,):")
    result = user_input.find(',') 
    print ("Substring ',' found at index:", result )

    if result != -1 and user_input != 'quit':
      bad_input = False
      key , value = user_input.split(",")
      key = key.strip()
      value = value.strip()
      my_dict[key] = value
      print (my_dict)
  if user_input == 'quit':
    print(my_dict)
    break

This is my code and I need it to find the ',' in the user input dictionary and also when you type 'quit' the program needs to end.这是我的代码,我需要它在用户输入字典中找到“,”,并且当您键入“退出”时,程序需要结束。 My teacher is not educated in python enough to help me, please help!我的老师没有受过python的教育足以帮助我,请帮助!

break only breaks the inner loop. break只打破内部循环。 In order to break out of the nested while loops without changing too much of your logic you will need to change the values that they check.为了在不改变太多逻辑的情况下跳出嵌套的while循环,您需要更改它们检查的值。

In this case, setting both bad_input and more to False will suffice:在这种情况下,将bad_inputmore都设置为False就足够了:

if user_input == 'quit':
    print(my_dict)
    more = False
    bad_input = False

With that said, you don't even need both loops.话虽如此,您甚至不需要两个循环。 Use a single while then you will be able to use break :使用一个while然后你就可以使用break

my_dict = dict()
while True:
    user_input = input("Enter key and value separated buy commas(,):")
    result = user_input.find(',')
    print("Substring ',' found at index:", result )

    if result != -1 and user_input != 'quit':
        bad_input = False
        key , value = user_input.split(",")
        key = key.strip()
        value = value.strip()
        my_dict[key] = value
        print (my_dict)
    if user_input == 'quit':
        print(my_dict)
        break

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 试图为我的 python 类完成这个程序,但无法弄清楚为什么我会收到 TypeError - Trying to finish this program for my python class, but can't figure out why I'm receiving a TypeError 我正在尝试为我的“情绪分析”项目制作前端,但不知道如何让我的“预测功能”在前端工作 - I'm trying to make Front-end for my “Sentiment Analysis” project but can't figure out how to make my “prediction_function” work at the Front-end 我正在尝试在要求用户输入两个输入的地方编写一行代码,但无法弄清楚如何完成此操作 - I'm trying to write a line of code at ask the user for two inputs but can't figure out how to complete this 我不知道如何将用户输入作为字符串处理 - I can't figure out how to handle user input as strings 我正在尝试使用模块 simplegui 在 CodeSkulptor 中制作一个带有两个输入字段的计算器。 我不知道我做错了什么 - I'm trying to make a calculator with two input fields in CodeSkulptor using the module simplegui. I can't figure out what I am doing wrong 我正在尝试制作一个名称由用户输入定义的字典。 我该怎么做呢? - I'm trying to make a dictionary whose name is defined by user input. How do I do this? 我正在尝试结束我的程序,但我不知道该怎么做 - I'm trying to end my program but I don't know how to do it 我需要弄清楚如何使我的程序重复。 (Python编码类) - I need to figure out how to make my program repeat. (Python coding class) 我试图了解类和函数,但似乎无法弄清楚我的代码出了什么问题 - I am trying to understand class and function and can't seem to figure out what is wrong with my code 我不知道如何在Python中创建调用我的函数的字典 - I can't figure out how to create a dictionary in Python that calls my functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM