我无法找到一种方法来查找字典是否为空以及如何将其用于if循环的条件,
这是我目前的if循环:
if del_question == "2":
我正在努力使它有一个额外的条件,以便在字典为空时不能进入if循环。
这是我开头的词典:
dictionary = {}
用户可以输入和删除键和值对,也可以删除它们,如果条件允许用户删除它们的配对,但我不想让它们卡在内部的while循环中他们没有任何配对。
感兴趣的人的完整循环:
if del_question == "2":
print('These are you letter and symbol pairings', dictionary)
del_letter = input("Please input the symbol you wish to unpair: ")
while len(del_letter) != 1:
print(del_letter, 'is not a valid input please try again or type "end" if you do not want to change a pairing')
del_letter = input("Please input the symbol you wish to unpair: ")
del_symbol = dict.get(dictionary, del_letter)
words = words.replace(del_symbol, del_letter)
del dictionary[del_letter]
print(words)
答案 0 :(得分:3)
if dictionary:
任何具有非零len
的内容都被视为布尔值为true,任何零len
的内容都被视为false(除非对象具有__bool__
方法,使用不同的规则)。