2 回答
TA贡献1794条经验 获得超7个赞
你的大问题是range函数写错了,你在较低的值之前输入了较高的值,这是错误的。与这个问题相比,您的其他问题可能是次要的,但仍然非常重要!确保始终检查您的输入以避免意外行为,例如负数或零。修复方法是:
lst = []
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:
print('Cannot receive a length lower than 1!')
MaxQueue = int(input('\nSet The Maximum Queue to: '))
for i in range(0, MaxQueue):
print(lst)
inn = input('Enter Name: ')
lst.append(inn)
print('\n')
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? (yes/no):'))
for i in range(0, MaxQueue):
print(lst)
input('press (enter) to serve') # no need to save input.
print(lst.pop(0))
TA贡献1963条经验 获得超6个赞
起点超过终点的范围是空的。您get_answer还包含一些错误。
lst = []
print(lst)
print('The queue is now empty...')
MaxQueue = int(input('\nSet The Maximum Queue to: '))
for i in range(MaxQueue):
print(lst)
inn = input('Enter Name: ')
lst.append(inn)
print('')
print(lst)
print('The Queue is full..')
def get_answer(prompt):
answer = None # set initial value to make sure the loop runs at least once
while answer not in ('yes', 'no'):
answer = input(prompt)
if answer == 'no':
exit()
get_answer('Do you want to start serving? ')
for i in range(MaxQueue):
print(lst)
input('press (enter) to serve')
print(lst.pop(0))
对于较大的程序,放在中间通常不是一个好主意exit(),因为您可能想做其他事情,所以我们可以改用布尔逻辑并做类似的事情
def get_answer(prompt):
answer = None # set initial value to make sure the loop runs at least once
while answer not in ('yes', 'no'):
answer = input(prompt)
return answer == 'yes'
if get_answer('Do you want to start serving? '):
for i in range(MaxQueue):
print(lst)
input('press (enter) to serve')
print(lst.pop(0))
添加回答
举报