Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 循环赢了';dict为空时不能退出_Python_Loops - Fatal编程技术网

Python 循环赢了';dict为空时不能退出

Python 循环赢了';dict为空时不能退出,python,loops,Python,Loops,使用Python 3: def show_flashcard(): """ Show the user a random key and ask them to define it. Show the definition when the user presses return. it then asks if user knew definition or not if they do delets the word and

使用Python 3:

def show_flashcard():    
    """ Show the user a random key and ask them
        to define it. Show the definition
        when the user presses return. it then asks if user
        knew definition or not if they do delets the word and
        then asks if they want to continue or quit.
    """
    random_key = choice(list(glossary))
    print('Define: ', random_key)
    input('Press return to see the definition')
    print(glossary[random_key])
    import time
    time.sleep(1) # delay for 1 seconds
    print('Did you know the answer')
    user_input = input('y or n: ')
    if user_input == 'n':
        print( 'Do not give  up you can only learn by practice')
        time.sleep(1) # delay for 1 seconds
        user_input = input('Enter s to show a flashcard and q to quit: ')
    if user_input == 'y':
        print( 'Congratulations , the word wil now be removed from the dictionary')
    del (glossary[random_key])

# Set up the glossary
glossary = {'1: word1':'definition1',
            '2: word2':'definition2',
            '3: word3':'definition3'}

# The interactive loop
exit = False
while not exit:
    if glossary == {''}:
        exit = True   
    user_input = input('Enter s to show a flashcard and q to quit: ')
    if user_input == 'q':
        exit = True
    elif user_input == 's':
        show_flashcard()
    else:
        print('You need to enter either q or s.')

当glossary为空时,我似乎无法让这个循环自动退出。我已经尝试了很多东西,从
如果glossary=0,那么exit为true,但我似乎什么都做不到。这让我抓狂。

如果glossary=={'}:
退出条件,
将永远不会为真,因为您正在将glossary(一个
dict
)与包含单个空字符串元素的进行比较

您可以在条件中直接使用
dict
对象,如果该对象为空,它将计算为
False
。为了立即退出循环,您还可以使用:

while True:
    if not glossary:
        break
    user_input = input('Enter s to show a flashcard and q to quit: ')
    if user_input == 'q':
        break
    elif user_input == 's':
        show_flashcard()
    else:
        print('You need to enter either q or s.')

退出条件,
如果glossary=={'}:
,将永远不会为真,因为您正在将glossary(一个
dict
)与包含单个空字符串元素的进行比较

您可以在条件中直接使用
dict
对象,如果该对象为空,它将计算为
False
。为了立即退出循环,您还可以使用:

while True:
    if not glossary:
        break
    user_input = input('Enter s to show a flashcard and q to quit: ')
    if user_input == 'q':
        break
    elif user_input == 's':
        show_flashcard()
    else:
        print('You need to enter either q or s.')

通过以下方式,它可以更加精简:

while glossary:
    user_input = ...

通过以下方式,它可以更加精简:

while glossary:
    user_input = ...

有些人可能知道您使用的是什么编码语言,但实际上您并没有在帖子的任何地方说明使用哪种语言。确保也为您的语言添加标记。此外,请查看并获取有关SO工作原理的更多信息。祝你好运在函数定义的一开始,您还缺少一行代码。有些人可能知道您使用的是哪种编码语言,但实际上您没有在帖子的任何地方说明哪种语言。确保也为您的语言添加标记。此外,请查看并获取有关SO工作原理的更多信息。祝你好运在函数定义的一开始,您还缺少一行代码。不客气。如果你觉得这是有用的,请考虑你的问题的答案。不客气。如果你发现这是有用的,请考虑你的问题的答案。