空列表检查python(循环错误)

问题描述 投票:0回答:3

如果列表为空,我想输出消息“不再剩饼干”

我尝试过,但是一直循环播放(由于while循环),您能帮我解决这个问题吗?>>
more = input('\nWould you like a biscuit (Y/N)... ')

while more != 'N':

    if len(biscuits) != 0 :
        choice = random.randint(0,len(biscuits)-1)
        print('Your biscuit is : ', biscuits[choice])
        del biscuits[choice]
        more = input('\nWould you like a biscuit (Y/N)...')
    else:
        print("No more biscuits are left")

print('\nThere are ', len(biscuits), ' biscuits left.')
print('\n', biscuits, '\n')

如果列表为空,我尝试输出消息“不再剩饼干”,但是它一直循环播放(由于while循环)您能帮我解决更多问题吗= input('\ n您想要吗一个...

python
3个回答
1
投票

在打印后使用中断,它将从循环中中断并结束。


0
投票

将两个条件都拉到while循环中:


-1
投票

在python中,一个空列表将具有与False相等的布尔值,因此您可以简单地这样做:

© www.soinside.com 2019 - 2024. All rights reserved.