Python字典在向其添加项的函数完成执行后立即变为空
我正在尝试编写一个菜单,其中选项1将向字典添加一个key:value对,选项2将使用字典中的项运行线程模块。键是来自用户输入的消息,值是来自用户输入的线程在显示消息之前应该暂停的秒数 不过,线程本身目前并不重要。我正在努力解决的是,当我使用选项1的函数时,它成功地将key:value对添加到字典中(称为messages_和_times)。但是,一旦函数完成,字典就会再次变为空,从选项2的函数中可以看出,该函数访问字典 在底部你可以看到我的代码。为了在每个步骤中检查字典中的内容,我添加了以下几行:Python字典在向其添加项的函数完成执行后立即变为空,python,python-3.x,list,function,dictionary,Python,Python 3.x,List,Function,Dictionary,我正在尝试编写一个菜单,其中选项1将向字典添加一个key:value对,选项2将使用字典中的项运行线程模块。键是来自用户输入的消息,值是来自用户输入的线程在显示消息之前应该暂停的秒数 不过,线程本身目前并不重要。我正在努力解决的是,当我使用选项1的函数时,它成功地将key:value对添加到字典中(称为messages_和_times)。但是,一旦函数完成,字典就会再次变为空,从选项2的函数中可以看出,该函数访问字典 在底部你可以看到我的代码。为了在每个步骤中检查字典中的内容,我添加了以下几行:
print(dict(messages_and_times))
if not messages_and_times:
print("This dictionary is empty.")
else:
print("This dictionary contains items.")
然而,它似乎也不能正常工作。首先,无论打印的字典是否为空,它都会打印“此字典包含项目”。下面代码的第二部分(clear()用于清除终端显示):
如果我选择添加项目,则打印包含项目的词典。但是如果我加上一行
print(dict(messages_and_times))
对于main_menu()本身,上述create_dictionary()函数将打印一个空字典。只有主菜单()中的这条print()语句会影响create\u dictionary()是否显示包含项的词典
有人能帮助我理解如何设计一个代码,其中字典保留一个函数创建的项,以便其他函数可以访问它们吗
提前感谢您的时间和帮助
import os
clear = lambda: os.system('cls')
def主菜单():
消息和时间
不是dict
,而是zip
对象。请参阅:messages\u和\u times=zip(消息列表,时间列表)
zip
对象只能迭代一次。啊,我明白了。将这两个用户输入合并到一个我可以多次迭代的字典中的另一种有效方法是什么?。。。实际创建一个字典。同样,您只需创建一个zip
对象,然后将其耗尽。你确实用它创建了一个dict
,但你只需打印它,然后它就会被立即回收,因为它没有任何引用。messages\u和\u times=dict(zip(list\u of messages,list\u of\u times))
@Bakuriu这一个看起来也像zip本身的行为,所以它不起作用。不过我已经解决了。这两个用户输入现在被分配给变量“message”和“seconds”,后面是行“messages_和_times[message]=seconds”。最后,我将消息_和_乘以一个全局变量,它现在可以工作了。非常感谢你们让我走上正轨,伙计们!
import os
clear = lambda: os.system('cls')
list_of_messages = []
list_of_times = []
messages_and_times = zip(list_of_messages, list_of_times)
def option_1():
clear()
list_of_messages.append(
input("Please type in a message you would like to add to the list:"))
clear()
list_of_times.append(
input("Please type in the time of delay for this message:"))
def create_dictionary():
clear()
answer = input(
"Would you like to add another message? (yes/no)").lower()
if answer == "yes":
option_1()
elif answer == "no":
clear()
print("You will now be returned to the main menu.")
print(dict(messages_and_times))
if not messages_and_times:
print("This dictionary is empty.")
else:
print("This dictionary contains items.")
time.sleep(1.5)
main_menu()
else:
clear()
print("Please answer yes or no.")
time.sleep(1.5)
create_dictionary()
create_dictionary()
def option_2():
clear()
print(dict(messages_and_times))
if not messages_and_times:
print("This dictionary is empty.")
else:
print("This dictionary contains items.")
time.sleep(5)
main_menu()
clear()
selection = 0
while selection == 0:
print(("-" * 15) + "MAIN MENU" + ("-" * 15) + "\n")
print("1: Input a message and a corresponding time of delay before its display.")
print("2: Print your customized list of messages.")
print("3: Generate a list of random messages with random delays.\n")
selection = int(input(
"Please select one of the options, by typing in the corresponding number:"))
if selection == 1:
option_1()
elif selection == 2:
option_2()
elif selection == 3:
clear()
print("You've selected the third option.")
else:
clear()
print("Please select from options 1 - 3.\n")
time.sleep(1.5)
main_menu()