簡體   English   中英

如何存儲元組並繼續要求用戶輸入更多值,直到用戶輸入“完成”?

[英]How to store tuples and continue to ask the user to input more values until user inputs 'done'?

因此,任務是創建一個元組列表,其中存儲了運行日期,運行了多長時間以及在該日期運行了多少英里。 程序應繼續要求用戶輸入有關運行時間的信息,直到用戶輸入“完成”為止。 輸出應該是存儲在名為“ run_data”的列表中的所有元組。 這是我所擁有的:

def data(date, time, distance):

    list1 = [(date, time, distance)]
    done = False
    while input == done:
        run_data = list1.append((date, time, distance))
def main():

    d = input('input the date of your run in the form mmdd: ')
    t = input('input how long your run was in minutes: ')
    m = input('input the distance you ran in miles: ')

    running = data(d, t, m)
    print(running) 
main()

輸入時我沒有任何錯誤,但是在詢問距離之后,它什么也沒有輸出。 我很困惑為什么它輸出了那個,並且不確定我哪里出錯了。 我想提一下,我是使用python的初學者,因此將非常感謝您的幫助。 謝謝!

看看這個。 將控件放入main,循環直到“完成”。

def data(date, time, distance):
    return (date, time, distance)


def main(tList):
    while true:
        d = input('input the date of your run in the form mmdd: ')
        if d == 'done':
            break
        t = input('input how long your run was in minutes: ')
        m = input('input the distance you ran in miles: ')
        tList.append(data(d, t, m))


timeList = []
main(timeList)
print(timeList)

要連續獲取輸入,您可以將main編寫為:

list1 = []
while True:
       d = input('input the date of your run in the form mmdd: ')
       t = input('input how long your run was in minutes: ')
       m = input('input the distance you ran in miles: ')

       list1.append((d, t, m))
       # YOu can ask the user if you want to add more data.
       print "Would you like to add more data"
       done = raw_input()
       if(done == "yes" or "y"):
           continue
       else:
           break

print list1

這樣,您可以連續將數據添加到列表中,最后打印列表。 編碼愉快!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM