简体   繁体   中英

Is there a way to print a variable x times depending on what number the user wrote in the input to decide x? [Python 3.8]

I'm making a random name generator using the random module. I'm using the most popular first female and male names along with popular last names to generate two names, one male, and one female:

firstnames = ['Liam', 'Noah', 'William', 'Oliver', 'Benjamin', 'Elijah', 'Lucas', 'Mason', 'Logan']
lastnames = ['Smith', 'Johnson', 'Williams', 'Jones', 'Garcia', 'Miller', 'Davis', 'Wilson', 'Martinez']
print(random.choice(firstnames) + " " + random.choice(lastnames))

firstfemnames = ['Emma','Olivia','Ava', 'Isabella', 'Sophia', 'Charlotte', 'Mia', 'Amelia', 'Harper', 'Evelyn']
lastfemnames = ['Smith', 'Johnson', 'Williams', 'Jones', 'Garcia', 'Miller', 'Davis', 'Wilson', 'Martinez']
print(random.choice(firstfemnames) + " " + random.choice(lastfemnames))

So the output, after being printed, would give two random names like this:

Noah Jones
Evelyn Smith

I want to make an input for how many other random names they want after those two are printed; for example, if you make an input after printing the first two random names [Noah Jones and Evelyn Smith], I want to make an input for numbers, so if you input 2, another two random names are printed. How do I do this?

import random

firstnames = ['Liam', 'Noah', 'William', 'Oliver', 'Benjamin', 'Elijah', 'Lucas', 'Mason', 'Logan']
lastnames = ['Smith', 'Johnson', 'Williams', 'Jones', 'Garcia', 'Miller', 'Davis', 'Wilson', 'Martinez']


firstfemnames = ['Emma','Olivia','Ava', 'Isabella', 'Sophia', 'Charlotte', 'Mia', 'Amelia', 'Harper', 'Evelyn']
lastfemnames = ['Smith', 'Johnson', 'Williams', 'Jones', 'Garcia', 'Miller', 'Davis', 'Wilson', 'Martinez']



def printMaleName():
    print(random.choice(firstnames) + " " + random.choice(lastnames))

def printFemaleName():
    print(random.choice(firstfemnames) + " " + random.choice(lastfemnames))

male = int(input("Enter the number of male names you want: "))
female = int(input("Enter the number of female names you want: "))

for _ in range (male):
    printMaleName()
for _ in range(female):
    printFemaleName()

For getting the number of pairs, use the input() function

For acting on it, use a for loop.


import random
firstnames = ['Liam', 'Noah', 'William', 'Oliver', 'Benjamin', 'Elijah', 'Lucas', 'Mason', 'Logan']
lastnames = ['Smith', 'Johnson', 'Williams', 'Jones', 'Garcia', 'Miller', 'Davis', 'Wilson', 'Martinez']
print(random.choice(firstnames) + " " + random.choice(lastnames))

firstfemnames = ['Emma','Olivia','Ava', 'Isabella', 'Sophia', 'Charlotte', 'Mia', 'Amelia', 'Harper', 'Evelyn']
lastfemnames = ['Smith', 'Johnson', 'Williams', 'Jones', 'Garcia', 'Miller', 'Davis', 'Wilson', 'Martinez']
print(random.choice(firstfemnames) + " " + random.choice(lastfemnames))

noofchildren=input()
for i in noofchildren:
    print(random.choice(firstfemnames) + " " + 
    random.choice(lastfemnames))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM