Beginner issue with random.choice

Viewed 46

I want to create a list of students for each class, and then be able to randomly select a student.

import random

period1 = ['bern', 'brian', 'molly', 'pizza', 'dave', 'deena', 'tom', 'kelly']

period2 = ['bob', 'shane', 'marge', 'frank', 'Becky ', 'Delilah ', 'Teddy', 'Hudson']

random_student = input('Which class needs a volunteer?')

print(random.choice(random_student))
1 Answers

I think you will be better off creating a dictionary rather than using lists.

 import random

period1 = ['bern', 'brian', 'molly', 'pizza', 'dave', 'deena', 'tom', 'kelly']

period2 = ['bob', 'shane', 'marge', 'frank', 'Becky ', 'Delilah ', 'Teddy', 'Hudson']

random_student = input('Which class needs a volunteer?')

d = {'period1': period1, 'period2': period2}

print(random.choice(d[random_student]))

input returns a string and not a variable. For example input will return 'period1' as a string while you want to call the variable period1 One way to get around this is to create a dictionary using a key and value pair. The key will be a string: 'period1' and the values will be a list of student names. Now you will be able to filter to the list you want with a string.

Related