Reputation: 59
Hello I'm fairly new to python but I'm trying to figure out how to assign multiple variables (in this case name and an ID number) that's already inside of a loop.
This is the example of the loop as I'm making a program that puts people in a different teams manually and then printing the current teams.
The input should be the name and ID number. So far I've tried this but don't know where to go from here. Perhaps putting them into a dictionary and somehow assigning them to a team?
team_size = int(input('What is the team size: '))
for i in range(team_size):
num = num + 1
print(f'Enter students for team {num}:')
temp = input().split(' ')
manual_dict.update({temp[0]: temp[1]})
Upvotes: 1
Views: 73
Reputation: 144
You can just assign result of split to multiple variables:
from collections import defaultdict
manual_dict = defaultdict(list)
n_teams = int(input('How many teams you want to enter: '))
for num in range(n_teams):
team_size = int(input(f'What is the team #{num} size: '))
for i in range(team_size):
print(f'Enter #{i} student name and id for team #{num}:')
name, user_id = input().split(' ')
user_id = int(user_id)
manual_dict[num].append({name: user_id})
print(dict(manual_dict))
Result (output):
How many teams you want to enter: >? 2
What is the team #0 size: >? 1
Enter #0 student name and id for team #0:
>? Jeremy 123
What is the team #1 size: >? 2
Enter #0 student name and id for team #1:
>? Emily 234
Enter #1 student name and id for team #1:
>? Joshua 345
{0: [{'Jeremy': 123}], 1: [{'Emily': 234}, {'Joshua': 345}]}
More information here
Upvotes: 1