简体   繁体   中英

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

So the assignment is to create a list of tuples that stores the date of the run, how long you ran, and how many miles you ran for that date. The program should continue to ask the user to input the information about running times until the user inputs "done". The output should be all of the tuples that were stored in the list called 'run_data'. Here is what I have:

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

I have no errors when it comes to inputting it, but after asking for the distance, it outputs none. I'm confused as to why it's outputting that and not sure where I went wrong. I would like to mention that I'm a beginner to using python so any help will be greatly appreciated. Thanks!

Have a look at this. Put the control in main, looping until "done".

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)

To get inputs continuously you can write in main as:

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

This way you can continuously add the data to list and finally print the lists. Happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM