-3

I am trying to write a function that asks the user how many days of sleep he wants to report, then according to the answer it should ask for the day of the week and the hours slept. The function must return a list of lists with the days of the week and the amount of hours the user reported sleeping.

So, if I call

print(healthy())
"How many days do you want to report?" :   3
Day of the week:   Monday
Hours slept:  6
Day of the week:   Tuesday
Hours slept:  8
Day of the week:   Wednesday
Hours slept:  7

Then it should print, a list of list with the information of the user:

[['Monday', 6], ['Tuesday', 8], ['Wednesday', 7]]

So far I have:

def healthy():
    record = []
    days = int(input("How many days do you wanna report?: "))
    DofW = input(str("which day?"))
    Hr = int(input("how many hours?"))
    for i in range(days):
        record.append(DofW)
        record.append(Hr)
    print(record)

but this is not giving what I want. Pls help!

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Johnny Lu
  • 1
  • 1
  • Welcome to Stack Overflow! Please take the [tour] and read [ask]. What's your question? Please [edit] to clarify. You might want to read [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/q/284236/4518341) – wjandrea Jul 03 '21 at 03:02
  • One problem with your code already has an existing question: [How is returning the output of a function different from printing it?](https://stackoverflow.com/q/750136/4518341) – wjandrea Jul 03 '21 at 03:09

4 Answers4

1

In your healthy(), you only asked "which day?" and "how many hours?" once, but according to your description, the asking should be put into the loop.

Besides, record is a list instead of a list of lists. What you want may be like this: record.append([DofW, Hr])

Lastly, if you want to call print(healthy()), then your healthy() is supposed to return something. Replace print(record) with return record, which is better practice anyway.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Zebartin
  • 124
  • 10
0

Ok, you need to actually return the record.

Also, you need to put the day in the for loop:

def healthy():
    record=[]
    days=int(input("How many days do you wanna report?: "))
    for i in range(days):
        DofW=input("\nwhich day? ")
        Hr=int(input("\nhow many hours? "))
        record.append([DofW,Hr])
    return record
print(healthy())
0
def healthy():
    record = []

    for i in range(int(input('how many days do you want to report?'))):
        record.append([input('which day?'), int(input('how many hours?'))]) 
print(record)

you need to loop through the amount of days, then each time the program asks for name of day and amount of hours, and pushes it into the record array

0

you are only asking for input once if you review your code. Secondly, you are appending item by item instead of appending a list.

def healthy():

    record=[]

    days=int(input("How many days do you wanna report?: "))

    #i changed variable names to make more sense
    for i in range(days):
        #This method of iteration will constantly ask for input
        day_input=input(str("Which day?"))

        hours_input=int(input("how many hours?"))

        #Append in a list of [day,hours]
        record.append([day_input,hour_input])
        
    print(record)
Fishball Nooodles
  • 474
  • 1
  • 4
  • 11