亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 def 函數之后運行 for 循環?

如何在 def 函數之后運行 for 循環?

蕭十郎 2023-03-16 10:04:01
我試圖在 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))
查看完整描述

2 回答

?
慕田峪9158850

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))


查看完整回答
反對 回復 2023-03-16
?
神不在的星期二

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))


查看完整回答
反對 回復 2023-03-16
  • 2 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號