简体   繁体   English

有没有办法根据用户在输入中写入的数字来决定 x 打印变量 x 次? [Python 3.8]

[英]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.我正在使用random模块制作一个随机名称生成器。 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:所以 output 在打印后会给出两个这样的随机名称:

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.例如,如果您在打印前两个随机名称 [Noah Jones 和 Evelyn Smith] 后进行输入,我想输入数字,因此如果您输入 2,则会打印另外两个随机名称。 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要获取对数,请使用input() function

For acting on it, use a for loop.要对其进行操作,请使用for循环。


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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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