如何在 def 函数之后运行 for 循环?

我试图在 def 函数之后运行最后一个 for 循环,但它并没有像我想的那样运行。


lst = [] 


print(lst)

print('The queue is now empty...')


MaxQueue = int(input('\nSet The Maximum Queue to: '))


for i in range(0, MaxQueue): 

    print(lst)

    inn = input('Enter Name: ')

    lst.append(inn)  

print('')   

print(lst) 

print('The Queue is full..')   


def get_answer(prompt):

    while True:

        answer = input(prompt)

        if answer not in ('yes','no'):

            answer = input(prompt)        

        if answer in ('yes'):

            break         

        if answer in ('no'):

            exit()

    

print(get_answer('Do you want to start seriving? '))


for i in range(MaxQueue,0):

    print(lst)

    de = input('press (enter) to serve')

    print(lst.pop(0))


萧十郎
浏览 107回答 2
2回答

慕田峪9158850

你的大问题是range函数写错了,你在较低的值之前输入了较高的值,这是错误的。与这个问题相比,您的其他问题可能是次要的,但仍然非常重要!确保始终检查您的输入以避免意外行为,例如负数或零。修复方法是:lst = []&nbsp;print(lst)print('The queue is now empty...')MaxQueue = int(input('\nSet The Maximum Queue to: '))# A loop to ensure the user will never be able to insert a value lower than 1.while MaxQueue <= 0:&nbsp; &nbsp; print('Cannot receive a length lower than 1!')&nbsp; &nbsp; MaxQueue = int(input('\nSet The Maximum Queue to: '))for i in range(0, MaxQueue):&nbsp;&nbsp; &nbsp; print(lst)&nbsp; &nbsp; inn = input('Enter Name: ')&nbsp; &nbsp; lst.append(inn)print('\n')&nbsp; &nbsp;print(lst)&nbsp;print('The Queue is full..')&nbsp; &nbsp;def get_answer(prompt):&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp; answer = input(prompt)&nbsp; &nbsp; &nbsp; &nbsp; if answer not in ('yes','no'):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answer = input(prompt)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if answer in ('yes'):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if answer in ('no'):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit()&nbsp; &nbsp;&nbsp;print(get_answer('Do you want to start seriving? (yes/no):'))for i in range(0, MaxQueue):&nbsp; &nbsp; print(lst)&nbsp; &nbsp; input('press (enter) to serve') # no need to save input.&nbsp; &nbsp; print(lst.pop(0))

神不在的星期二

起点超过终点的范围是空的。您get_answer还包含一些错误。lst = []&nbsp;print(lst)print('The queue is now empty...')MaxQueue = int(input('\nSet The Maximum Queue to: '))for i in range(MaxQueue):&nbsp;&nbsp; &nbsp; print(lst)&nbsp; &nbsp; inn = input('Enter Name: ')&nbsp; &nbsp; lst.append(inn)&nbsp;&nbsp;print('')&nbsp; &nbsp;print(lst)&nbsp;print('The Queue is full..')&nbsp; &nbsp;def get_answer(prompt):&nbsp; &nbsp; answer = None # set initial value to make sure the loop runs at least once&nbsp; &nbsp; while answer not in ('yes', 'no'):&nbsp; &nbsp; &nbsp; &nbsp; answer = input(prompt)&nbsp; &nbsp; if answer == 'no':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; exit()&nbsp; &nbsp;&nbsp;get_answer('Do you want to start serving? ')for i in range(MaxQueue):&nbsp; &nbsp; print(lst)&nbsp; &nbsp; input('press (enter) to serve')&nbsp; &nbsp; print(lst.pop(0))对于较大的程序,放在中间通常不是一个好主意exit(),因为您可能想做其他事情,所以我们可以改用布尔逻辑并做类似的事情def get_answer(prompt):&nbsp; &nbsp; answer = None # set initial value to make sure the loop runs at least once&nbsp; &nbsp; while answer not in ('yes', 'no'):&nbsp; &nbsp; &nbsp; &nbsp; answer = input(prompt)&nbsp; &nbsp; return answer == 'yes'&nbsp; &nbsp;&nbsp;if get_answer('Do you want to start serving? '):&nbsp; &nbsp; for i in range(MaxQueue):&nbsp; &nbsp; &nbsp; &nbsp; print(lst)&nbsp; &nbsp; &nbsp; &nbsp; input('press (enter) to serve')&nbsp; &nbsp; &nbsp; &nbsp; print(lst.pop(0))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python