import random from pymongo import MongoClient mc = MongoClient("mongodb://localhost:27017") db = mc['starwars'] episode = input("What episode? (I-IX) ") thing = db.c.find({'episodes':episode}, {'name':1, '_id':0}) things = list(thing) print(f"Here are our Episode {episode} characters:") for i in range(len(things)): print(f"{i+1}) {things[i]['name']}") char_num = int(input("Choose a number for the character you would like more info about. ")) - 1 laur = things[char_num]['name'] print(f"I'd like to know more about {laur} too!") chosen_char = db.c.find({'name':laur}, {'_id':0, 'img':0, 'episodes':0}).next() if 'demeanor' in chosen_char: print(f"{chosen_char['name']} is a {chosen_char['demeanor']} {chosen_char['race']}.") else: print(f"{chosen_char['name']} is a {chosen_char['race']}.") random_key = random.choice(list(chosen_char.keys() - {'relations','episodes','name', 'race','demeanor','img'})) random_val = chosen_char[random_key] print(f"Fun fact about {chosen_char['name']}: their {random_key} is {random_val}.")