1

I really didn't know how to ask this question, and I probably sound stupid. I've been looking around for solutions to the problem I have been having, but everything I've tried hasn't worked. I'm working on a project for my computer science class, as my final, that randomly generates an identity from a list of names, birthdays, cities, addresses, etc. (sidenote: my teacher hasn't actually taught me anything; we read notes and did quizzes that consisted of definitions from the notes, and when we can't figure something out he just tells us to ask our neighbor. And, this is my first post on here, so if the formatting is incorrect, and my coding skills suck, I apologize in advance.) Anyway, I'll get on with my question/problem.

At the beginning of the program, it asks if you would like to generate a male or female identity:

def get_info():

global gender
global name
gender=input("Enter a gender of which you would like to randomly generate M=male F=female:")  
if gender==("F"):
    name=fname
elif gender==("M"):
    name=mname
else:
    print("Please enter either an M or F.")
    get_info()

which it then uses to determine which list the name comes from:

fname=random.choice(list(set(["girlynames","tina","caroline","etc"])))

or

mname=random.choice(list(set(["guynames","richard","earl","etc"])))

and then it prints out a random identity, including name, birthday, etc. like this:

Your new identity is: 
Nathaniel Ellis
Your birthday is :
September 5 1985
Your address is :
470 MacMurray Acres

and I have added a function, after the main part of the program, that asks for user input if they would like to use the program again.

def retry():

again=input("Would you like to generate another? Y/yes N/no: ")
if again==('Y'):
    get_info()
    generate_identity()
    retry()

elif again==('N'):
    print("Thanks for using the identity generator! Have a spectacular day")
    quit()

The problem is, every time I input 'Y', and a gender, it has the exact same results as it did the first time, even with a different gender all the results are the same besides the first name.

Your new identity is: 
Ava Ellis
Your birthday is :
September 5 1985
Your address is :
470 MacMurray Acres

I feel like there is a simple solution to this, but everything I've read and tried from this website (along with other sources) haven't worked. Is there a way to remove an item from a list after it has been used? Or make it so the results vary every time? Or even a way to just, like, reset Python, since it is only pseudorandom? I have been stumped on this for 3 days now, it is due by next monday, and I've been here on stackoverflow trying to find a solution since I noticed the problem. Help will be greatly appreciated and you'll be a life saver! :)

  • How are you generating the address? You forgot to post that part of your code. – Burhan Khalid Jun 22 '16 at 06:49
  • @Burhan For addresses, it uses the same concept as the name generator, other than for the house number, and also for birthday/year. For those, i have random.randint. – goochygucci Jun 23 '16 at 01:17

2 Answers2

1

You can remove a random item from a list with pop.

selection = random.choice(range(len(name_list)))
name = name_list.pop(selection)

This selects a random item from name_list, removes it, and assigns it to name.

That said, it looks like your random selection is working fine but you are not reassigning the value of name correctly. But you would need to post more of your code to know for sure. Double check that after retry is run you are both reassigning fname/mname and reassigning that to name, in that order.

rurp
  • 1,376
  • 2
  • 14
  • 22
  • At first, I was having troubles understanding this solution, but eventually, I figured it out and now it's working exactly how I wanted it to! This is exactly what I was looking for, and I'm so relieved that I got such a fast reply! Thank you very much rurp – goochygucci Jun 23 '16 at 00:39
  • Great! Glad I could help. – rurp Jun 23 '16 at 00:53
  • :) I forgot to add this, but is there any way to do it with a random.randint number? I have this bdayear=random.randint(1970,1999) to generate a birth year and i can't use the same thing shown above, is there a simple solution to this as well? – goochygucci Jun 23 '16 at 01:30
  • Sure, set the years to a list, ```years = [x for x in range(1970, 2000)]```. Then use ```years``` just like I used ```name_list``` above. – rurp Jun 23 '16 at 01:44
-1

What if you set the seed to a random number every time you launch the program, so your random.choice option would differ every time?

import random
seed = random.randint(0,9999)
random.seed(seed)
Dhia
  • 10,119
  • 11
  • 58
  • 69
ljc
  • 943
  • 2
  • 10
  • 26