1

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]}) 
1
  • all of these students are being drawn from an original roster, I'm just trying to put them into teams without taking the input of how many teams there are. the program should stop after there are no many names/ID in the original roster. Commented Nov 5, 2019 at 16:58

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

ok, but is there a way to assign ({name: user_id}) to the team number that it is put into?
@sunny do you want to support multiple teams? There is no such functionality in your code.
yes, in the example I would like to support multiple teams, I'm not sure how to assign the name/ID into teams that being looped (i.e. team 1/2/3) and then printing them out.
ah, is there a way to do it without asking how many teams there are?
@sunny you can define n_teams as constant without input.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.