I'm making a simple app that will spit out a random item from a list based on the category. I have two categories currently (places, and names). I'm trying to create two functions. One will return one of the categories randomly (working correctly) and one will take the random category and then randomly pull from the list for that category.
My problem is instead of the get_response
function returning a value from the list, it returns a random character from the name of the list it is getting as an argument. Any ideas on how to get around this. Thanks
Here is my code:
from random import randrange
types = ["places", "names"]
places = ["The Upper Room", "Jerusalem", "Church", "Rome"]
names = ["Jesus", "Desciples", "Paul"]
def get_random_type():
return str(types[randrange(0, len(types))])
def get_response(chosentype):
return chosentype[randrange(0, len(chosentype))]
randtype = get_random_type()
print(randtype)
print(get_response(randtype))
EDIT: Thank you everyone for your help! What a great community!
Here is my final working code. It is a combination of multiple answers.
import random
categories = ["places", "names"]
options = dict(places = ["The Upper Room", "Jerusalem", "Church", "Rome"],
names = ["Jesus", "Desciples", "Paul"])
def get_random_type():
return random.choice(categories)
def get_response(chosen_category):
category_items = options[chosen_category]
return random.choice(category_items)
print(get_response(get_random_type()))