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))
添加回答
舉報