![]() |
Entry field random pull from list, each return own line - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Entry field random pull from list, each return own line (/thread-44037.html) |
Entry field random pull from list, each return own line - Bear1981 - Feb-23-2025 Extremely new to python and programming. I am working on creating a user interface that will pull from defined lists to generate random names for table top RPG application, or any other form of RPG. I have been able to create a basic interface with defined libraries to be pulled from. So currently a request of 1 per entry box will generate a random creation off of the tables but prints with spacing between each variable returned. IE: Omar yraen Ouss ar. What I am trying to get returned would be Omaryraen Oussar. If more than 1 is entered into the entry field it returns each section from the libraries in order. IE: Irenn Cal eil fin Crim Alean neld lyl. The outcome for more than 1 should read as follows. Irenneil Crimneld and Calfin Aleanlyl. Any thoughts or ideas on how to fix this to generate the desired outcome? I have posted the code below to be looked at as well. import tkinter as tk import random def generate_random_value(): try: num_values = int(entry.get()) if num_values > 0 and num_values <= len(demp): random_values = random.sample(demp,num_values) random_values2 = random.sample(dems,num_values) rv3 = random.sample(dehp,num_values) rv4 = random.sample(dehs,num_values) new_window = tk.Toplevel(window) combined_item = random_values+random_values2 new_window.title("Wow!!") xresult_label=tk.Label(new_window, text='result') xresult_label.grid() xresult_label.config(text=random_values+random_values2+rv3+rv4) else: result_label.config(text="Invalid input") result_label.grid_forget() except ValueError: result_label.config(text="Invalid input") try: num_values = int(entry2.get()) if num_values > 0 and num_values <= len(dems): random_values3 = random.sample(defp, num_values) rv5 = random.sample(defs,num_values) rv6 = random.sample(dehp,num_values) rv7 = random.sample(dehs,num_values) yresult_label=tk.Label(new_window, text='result') yresult_label.grid() yresult_label.config(text=random_values3+rv5+rv6+rv7) else: result_label.config(text="Invalid input") result_label.grid_forget() except ValueError: result_label.config(text="Invalid input") window=tk.Tk() window.title("Random Value Generator") #Dark Elf #Male prefix demp = ['Alak','Alt','Alton','Antr','Amal','Bar','Bel','Bhen','Berg','Brar','Cal','Chasz','Cor','Dant','Dhaun','Danv','Din','Dur','Elk','Elaug','Erth','Geld','Ghaun','Gul','Hatch','Imd','Iren','Irenn','Kalan','Ker','Kron','Kren','Krer','Mer','Mern','Niln','Nilo','Nym','Nyr','Omar','Phar','Phyx','Sor','Quev','Quen','Rinn','Riz','Ryl','Tsa','Tsab','Teb','Tlu','Tluth','Torr','Vel','Vell','Vuz','Vorn','Zak'] #Male suffix dems = ['agh','ahn','aun','afein','al','axle','daer','drin','eld','eil','fin','fein','fryn','ica','imar','inid','irahc','kah','myr','nas','nar','nozz','olin','omph','or','rar','rak','ril','tar','trin','trol','tro','vun','vex','ven','vyr','yl','yln','yrd','yraen','zaer','zen','zyr'] #Female prefix defp = ['Akor','Alon','Ang','Aun','Bae','Bar','Belar','Chal','Char','Chess','Dil','Dur','Dris','Elvan','Elv','Erel','Ethe','Felar','Faer','Felyn','Gin','Hael','Hal','Houn','Ilam','In','Iril','Irae','Ily','Ilm','Jan','Jhael','Lael','Lir','Lurd','Lua','Mua','Mae','Myr','Nhath','Nhil','Nes','Pallan','Phyr','Quar','Quilv','Rauv','Ril','Shri','Sur','Sin','Tal','Tiel','Triss','Vas','Vier','Vlon'] #Female suffix defs = ['a','ae','aer','afae','ala','ayna','baste','brin','cyce','cyril','dia','dah','dril','e','eyel','fryn','iara','idil','inrir','iri','iista','ithra','kiira','lay','lara','na','niss','nitra','onia','onyss','quarra','quesi','rae','raena','rina','ryna','shalee','sin','stra','tana','thara','vex','xena','xen'] #House prefix dehp = ['Alean','Ale','Arken','Auvrey','Baern','Barri','Cladd','Crim','De','Do','Des','Eils','Evreh','Helvi','Hla','Hun','Kin','Keil','Kae','Mae','Mel','Mye','Noqe','Noqu','Nili','Orly','Ouss','Rilyn','Takken','Tor','Zoa'] #House suffix dehs = ['alfin','ailin','ani','ana','ar','arn','ath','duis','deris','drar','ervs','epp','ett','ghym','iryn','irin','iros','lyl','mtar','mtor','ndar','neld','rae','rahel','rret','sek','th','tlar','tyl','und','urden','val','vyr'] entry = tk.Entry(window,width=3) entry.grid() entry2 = tk.Entry(window,width=3) entry2.grid() button = tk.Button(window, text="Generate", command=generate_random_value) button.grid() result_label = tk.Label(window, text="") result_label.grid() window.mainloop() RE: Entry field random pull from list, each return own line - deanhystad - Feb-23-2025 Divide and conquer. Break up a big task into multiple, simpler tasks. I would start by making a function that creates a random name. import random female_dark_elf_names = ( ('Akor', 'Alon', 'Ang', 'Aun', 'Bae', 'Bar', 'Belar', 'Chal', 'Char'), ('a', 'ae', 'aer', 'afae', 'ala', 'ayna', 'baste', 'brin', 'cyce') ) dark_elf_houses = ( ('Alean','Ale','Arken','Auvrey','Baern','Barri'), ('alfin','ailin','ani','ana','ar','arn','ath') ) def make_name(names, houses): return ( f"{random.choice(names[0])}{random.choice(names[1])} " f"{random.choice(houses[0])}{random.choice(houses[1])}" ) print(make_name(female_dark_elf_names, dark_elf_houses))Now it is easy to make multiple names. def make_names(names, houses, count=1): return [make_name(names, houses) for _ in range(count)] print(make_names(female_dark_elf_names, dark_elf_houses, 6))Using random.choice() you could end up with two elves with the same name and same house. Statistically very unlikely, but possible. To eliminate that possibility you can use sample instead of choice. Probably why your code uses sample. However, this also eliminates the possibility of two elves from the same house or two elves with the same name but from different houses. This is how you could write make_names using random.sample. def make_names(names, houses, count=1): np = random.sample(names[0], count) ns = random.sample(names[1], count) hp = random.sample(names[0], count) hs = random.sample(names[1], count) return [f"{np[i]}{ns[i]} {hp[i]}{hs[i]}" for i in range(count)]And if you want to make 3 male and 2 female names: new_characters = ( make_names(male_dark_elf_names, dark_elf_houses, 3) + make_names(female_dark_elf_names, dark_elf_houses, 2) )And once you have that working it is easier to tie it into your tkinter program. RE: Entry field random pull from list, each return own line - Pedroski55 - Feb-23-2025 How long do elves live? Maybe this is something like what you want? from secrets import choice def make_names(gender): if gender == 'm': name = choice(data['demp']) + choice(data['dems']) + ' ' + choice(data['dehp']) + choice(data['dehs']) return name elif gender == 'f': name = choice(data['defp']) + choice(data['defs']) + ' ' + choice(data['dehp']) + choice(data['dehs']) return name #Dark Elf # Omaryraen Oussar #Male prefix demp = ['Alak','Alt','Alton','Antr','Amal','Bar','Bel','Bhen','Berg','Brar', 'Cal','Chasz','Cor','Dant','Dhaun','Danv','Din','Dur','Elk','Elaug', 'Erth','Geld','Ghaun','Gul','Hatch','Imd','Iren','Irenn','Kalan','Ker', 'Kron','Kren','Krer','Mer','Mern','Niln','Nilo','Nym','Nyr','Omar', 'Phar','Phyx','Sor','Quev','Quen','Rinn','Riz','Ryl', 'Tsa','Tsab', 'Teb','Tlu','Tluth','Torr','Vel','Vell','Vuz','Vorn','Zak'] #Male suffix dems = ['agh','ahn','aun','afein','al','axle','daer','drin','eld','eil','fin', 'fein','fryn','ica','imar','inid','irahc','kah','myr','nas','nar','nozz', 'olin','omph','or','rar','rak','ril','tar','trin','trol','tro','vun', 'vex','ven','vyr','yl','yln','yrd','yraen','zaer','zen','zyr'] #Female prefix defp = ['Akor','Alon','Ang','Aun','Bae','Bar','Belar','Chal','Char','Chess', 'Dil','Dur','Dris','Elvan','Elv','Erel','Ethe','Felar','Faer','Felyn', 'Gin','Hael','Hal','Houn','Ilam','In','Iril','Irae','Ily','Ilm','Jan','Jhael', 'Lael','Lir','Lurd','Lua','Mua','Mae','Myr','Nhath','Nhil','Nes','Pallan','Phyr', 'Quar','Quilv','Rauv','Ril','Shri','Sur','Sin','Tal','Tiel','Triss','Vas','Vier','Vlon'] #Female suffix defs = ['a','ae','aer','afae','ala','ayna','baste','brin','cyce','cyril','dia','dah','dril', 'e','eyel','fryn','iara','idil','inrir','iri','iista','ithra','kiira','lay','lara','na', 'niss','nitra','onia','onyss','quarra','quesi','rae','raena','rina','ryna', 'shalee','sin','stra','tana','thara','vex','xena','xen'] #House prefix dehp = ['Alean','Ale','Arken','Auvrey','Baern','Barri','Cladd','Crim','De', 'Do','Des','Eils','Evreh','Helvi','Hla','Hun','Kin','Keil','Kae','Mae', 'Mel','Mye','Noqe','Noqu','Nili','Orly','Ouss','Rilyn','Takken','Tor','Zoa'] #House suffix dehs = ['alfin','ailin','ani','ana','ar','arn','ath','duis','deris','drar','ervs', 'epp','ett','ghym','iryn','irin','iros','lyl','mtar','mtor','ndar','neld', 'rae','rahel','rret','sek','th','tlar','tyl','und','urden','val','vyr'] # put all the bits in 1 place data = {d:[] for d in ['demp', 'dems', 'defp', 'defs', 'dehp', 'dehs']} bits = [demp, dems, defp, defs, dehp, dehs] count = 0 for key in data.keys(): data[key] = bits[count] count +=1 genders = ['f', 'm'] gender = 'x' while not gender in genders: print('Since Donald is president we don\'t do non-binary, you gotta be male or female! Enter m or f! ') gender = input('Are you male or female? Enter m or f ') name = make_names(gender) for gender in 'fmfmfm' : name = make_names(gender) print(f'How do you do {name}?')
RE: Entry field random pull from list, each return own line - Bear1981 - Feb-24-2025 Thank you both for the responses with code to reference as well as functionality of it. So the original idea is to have multiple races listed with a button for each. Once a race button is clicked the original thought is to open a submenu on the same page with Male/Female labels, with each followed by an entry box. I need to take a look at both examples that have been provided to see which one will work the most efficiently and allow for multiple names, based off the value in the entry box, to be generated off of user selection. One of the main reasons that I have started to work on this is throughout my time playing role playing games, either as a player or the story teller, the party will end up in a new town and go to either a inn or shop and immediately ask the DM or ST what the name is of the individual they are speaking to is. And lets face it, having to generate names on the fly is a giant pita. So the outcome of this is to make life easier. Again, thank you both. Once I figure out what will work best I will post an updated response to this thread. RE: Entry field random pull from list, each return own line - Bear1981 - Feb-24-2025 (Feb-23-2025, 07:24 AM)Pedroski55 Wrote: How long do elves live? RE: Entry field random pull from list, each return own line - Bear1981 - Feb-24-2025 (Feb-23-2025, 07:24 AM)Pedroski55 Wrote: How long do elves live? RE: Entry field random pull from list, each return own line - Pedroski55 - Feb-25-2025 This was just to give some examples of names: for gender in 'fmfmfm' : name = make_names(gender) print(f'How do you do {name}?')You just feed 'f' and then 'm' to the make_names() function to show some example names. You loop over the string 'fmfmfm' assigning gender to each letter in turn, then call the function with make_names(gender). You don't need this part, it was just to show example names. |