[英]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_input
和more
都设置为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.