0

I'm still a beginner at python, but I've been trying to get better at it lately. I'm trying to create a program that asks the user what day it is and what they have to do. It will then take this and place it in a list. I'm trying to get the program to print out the list that has the new assignment in it, but it's not working. (The list that has the assignment is in another list.)

I tried placing it all in a function and using a return stmt, but it says that "the object is not callable". I also tried normally printing it out with a print stmt, but it doesn't work.

#List of days
doa = ["M","T","W","TH","F","S","SU"]

#Lists for every day
my_list = []
for i in range(7):
    my_list.append([i])

#Ask user what today is
day = input("What is the day today?")

#Ask user assignment
ast = input("What do you have to do my sir?")

#Add to list
if day == doa[i]:
    my_list[i].append(ast)

#Print that days list to check
print(my_list(day))

I know what I'm doing is wrong. I'm assuming the way I'm doing it prints out the index for the list (that's in another list)...,but I couldn't think of any other way.

Yara z
  • 1

3 Answers3

1

I assume the user inputs TH for the day, so you can use list.index to get the index for the day and use that to append and print from.

doa = ["M","T","W","TH","F","S","SU"]

my_list = []
for i in range(7):
    my_list.append([i])

day = input("What is the day today?")
day_index = doa.index(day)

ast = input("What do you have to do my sir?")

my_list[day_index].append(ast)

print(my_list[day_index])
Jab
  • 26,853
  • 21
  • 75
  • 114
0

You can certainly do this using lists. Your approach of accessing the variables within the lists and even setting up your list of lists is likely what is tripping you up.

>>> doa = ["M","T","W","TH","F","S","SU"]
>>> my_list = [[day] for day in doa]
>>> my_list
[['M'], ['T'], ['W'], ['TH'], ['F'], ['S'], ['SU']]
>>> day = input("What is the day today?")
What is the day today?M
>>> ast = input("What do you have to do my sir?")
What do you have to do my sir?Too much 
>>> for d in my_list:
...     if d[0] == day:
...         d.append(ast)
... 
>>> my_list
[['M', 'Too much'], ['T'], ['W'], ['TH'], ['F'], ['S'], ['SU']]

What might be a better approach is to use a dictionary. This simplifies your code (and is generally more efficient) by providing named access to your lists.

>>> doa = ["M","T","W","TH","F","S","SU"]
>>> my_list = {day: [] for day in doa}
>>> my_list
{'M': [], 'T': [], 'W': [], 'TH': [], 'F': [], 'S': [], 'SU': []}
>>> day = input("What is the day today?")
What is the day today?M
>>> day
'M'
>>> ast = input("What do you have to do my sir?")
What do you have to do my sir?Too much
>>> ast
'Too much'
>>> my_list[day].append(ast)
>>> my_list[day]
['Too much']
>>> my_list
{'M': ['Too much'], 'T': [], 'W': [], 'TH': [], 'F': [], 'S': [], 'SU': []}
0

This works without major changes to your code:

#List of days
doa = ["M","T","W","TH","F","S","SU"]

#Lists for every day
my_list = []
for i in range(7):
    my_list.append([])

#Ask user what today is
day = input("What is the day today?")

#Ask user assignment
ast = input("What do you have to do my sir?")

#Add to list
my_list[doa.index(day)].append(ast)

#doa.index(day) is the the position of "day" in the list doa

#Print that days list to check
print(my_list[doa.index(day)])
Bytes2048
  • 344
  • 1
  • 6
  • 16