Reputation: 33
I am making a simple app that schedules a leagues games. I get the number of weeks played, and every team that week needs a matchup. Im not finished, but I was going through testing it the output generates weird.
def scheduleMaker(dicty):
weeks_to_sched = int(input("How many weeks?"))
#weeks count
weeks_count = [list(range(1, (weeks_to_sched + 1)))]
schedule = {}
#for every week of the season
for x in weeks_count:
#add a entry in the dict for this week
schedule[f"Week {x}"] = {}
#every team that week needs a matchup
for j in names:
#this team needs to be added to week 1 with a matchup
schedule[f"Week {x}"][j] = [j]
print(schedule)
scheduleMaker(teams)
I want a dictionary for each week created and then the key: values (matchups) in each week's dictionary. Im having trouble with the first lines of the code. Im getting:
{'Week [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]': {}
Instead of:
{'Week 1': {}, 'Week 2': {}, 'Week 3: {}', etc}
Upvotes: 1
Views: 57
Reputation: 2583
First of all you need to learn how to debug your code and find errors. Adding simple print lines will help you to spot problems
print(weeks_count)
print(x)
#produces
[[1, 2, 3]]
[1, 2, 3]
Which means that x
is equal to the whole array instead of expected int. That is because you added extra brackets to the list and made it array with list as 1st element of array
# not ok
weeks_count = [list(range(1, (weeks_to_sched + 1)))]
# ok
weeks_count = list(range(1, (weeks_to_sched + 1)))
# best
weeks_count = range(1, weeks_to_sched + 1)
But also you don't need to keep weeks_count list, as you may iterate directly through range (code below)
I would also recommend not missing input, some global names
variable and logic of creating schedule into 1 function. Better way is to pass all required args into this functions so it has one responsibility
final code and output:
def scheduleMaker(weeks_to_sched, names):
schedule = {}
for weekNum in range(1, weeks_to_sched + 1):
schedule[f"Week {weekNum}"] = {}
for j in names:
schedule[f"Week {weekNum}"][j] = [j]
print(schedule)
weeks_to_sched = int(input("How many weeks?"))
scheduleMaker(weeks_to_sched, ['a', 'b', 'c'])
{'Week 1': {'a': ['a'], 'b': ['b'], 'c': ['c']}, 'Week 2': {'a': ['a'], 'b': ['b'], 'c': ['c']}, 'Week 3': {'a': ['a'], 'b': ['b'], 'c': ['c']}}
Upvotes: 1
Reputation: 159
You need to use the range()
function to be able to loop the way you described.
Try doing this instead
def scheduleMaker():
weeks_to_sched = int(input("How many weeks?"))
schedule = {}
#for every week of the season
for x in range(1, weeks_to_sched + 1):
#add a entry in the dict for this week
schedule[f"Week {x}"] = {}
#every team that week needs a matchup
for j in names:
#this team needs to be added to week 1 with a matchup
schedule[f"Week {x}"][j] = [j]
print(schedule)
Upvotes: 0
Reputation: 9494
The problem is here:
weeks_count = [list(range(1, (weeks_to_sched + 1)))]
It creates a list inside of a list so when you iterate over weeks_count
you have only one element there (one element of the entire range).
Instead, change it to be:
weeks_count = list(range(1, (weeks_to_sched + 1)))
Bare in mind that in your case, you don't have to convert the range into a list (conversion to a list is useful when you want to iterate over the range more than once), so you can use it directly.
weeks_count = range(1, (weeks_to_sched + 1))
Upvotes: 1