简体   繁体   English

如何在python中多次运行函数

[英]How to run a function multiple times in python

I have a code: 我有一个代码:

import random

vowels, consonants='aeiou','bcdfghjklmnpqrstvwxyz'

terms = int(input("How many letters you want for your baby's name? "))

def babyname():
    j=[]
    for i in range(0, terms):
        k = input("Would you like a [v]owel or [c]onsonant: ")
        if k.lower() == 'v':
            j.append(random.choice(vowels))
        elif k.lower() == 'c':
            j.append(random.choice(consonants))
        else:
            print("Unknown Input: "+ k)
    for x in range(0, 10):
        print(''.join(j))

babyname()

Input: I can input a number say 5 for the number of letters and v or c for those number of letters. 输入:我可以输入数字,例如字母数字为5,字母数字为v或c。

Expected output: For the input, I want to generate the generated text j for 10 times each one with different texts. 预期的输出:对于输入,我想生成生成的文本j ,每次生成10次,每一次使用不同的文本。 For the example input, the expected output should be - sdfes gdadf nkadj like this 10 words. 对于示例输入,期望的输出应为-sdfes gdadf nkadj,像这10个字一样。

Output yielded: Instead of getting 10 different texts, I am getting an output like sdfes sdfes sdfes - the same text for 10 times. 输出结果:我得到的输出不是sdfes sdfes sdfes,而是10条相同的文本,而不是10条不同的文本。

How to solve this? 如何解决呢?

If you want to print 10 different names but asking the vowels or constant questions once, you would do something like: 如果要打印10个不同的名称,但只问一次元音或常数问题,则可以执行以下操作:

import random

vowels, consonants='aeiou','bcdfghjklmnpqrstvwxyz'

terms = int(input("How many letters you want for your baby's name? "))

def babyname():
    choices = []
    for i in range(terms):
        k = input("Would you like a [v]owel or [c]onsonant: ")
        choices.append(k)

    for x in range(10):
        j = []
        for k in choices:
            if k.lower() == 'v':
                j.append(random.choice(vowels))
            elif k.lower() == 'c':
                j.append(random.choice(consonants))
            else:
                print("Unknown Input: " + k)
        print(''.join(j))

babyname()

EDIT: Note that, if you do not input v or c with the code above, it would tell you 10 times it is wrong, and only after you have given all the inputs. 编辑:请注意,如果您不使用上面的代码输入vc ,它将告诉您10次错误,并且仅在您输入了所有输入之后。 Hence, something like this might be a better approach: 因此,类似这样的方法可能是更好的方法:

import random

vowels, consonants='aeiou','bcdfghjklmnpqrstvwxyz'

terms = int(input("How many letters you want for your baby's name? "))

def babyname():
    choices = []
    for i in range(terms):
        while True:
            k = input("Would you like a [v]owel or [c]onsonant: ")
            if k.lower() in ('v', 'c'):
                break
            else:
                print("Unknown Input: " + k)
        choices.append(k.lower())

    for x in range(10):
        j = []
        for k in choices:
            if k == 'v':
                j.append(random.choice(vowels))
            elif k == 'c':
                j.append(random.choice(consonants))
        print(''.join(j))

babyname()

Because you are joining the same j list 10 times in the loop: 因为您要在循环中加入相同的j列表10次:

for x in range(0, 10):
        print(''.join(j))

You are getting this because you are only composing the string once and printing it 10 times at the end the solution would be to loop the whole process 10 times. 之所以会这样,是因为您只编写一次字符串,最后将其打印10次,解决方案是将整个过程循环10次。 Like 喜欢

def babyname():
 for n in range(0,10):
  j=[]
  for i in range(0, terms):
    k = input("Would you like a [v]owel or [c]onsonant: ")
    if k.lower() == 'v':
        j.append(random.choice(vowels))
    elif k.lower() == 'c':
        j.append(random.choice(consonants))
    else:
        print("Unknown Input: "+ k)
  print(''.join(j))

babyname()

To loop the same output 10 times you can do 要将相同的输出循环10次,您可以执行

def babyname():
 inp=""
 for i in range(0, terms):
  k = input("Would you like a [v]owel or [c]onsonant: ")
  if k.lower() == 'v' || k.lower()=='c':
    inp+=k
  else:
    print("Unknown Input: "+ k)
 for n in range(0,10):
  j=[]  
  for v in inp:
   if k.lower()=='v':
    j.append(random.choice(vowels))
   elif k.lower() == 'c':
    j.append(random.choice(consonants)
   print(''.join(j))

You generate one string j and print it 10 times. 您生成一个字符串j并将其打印10次。 Try to store 'v' and 'c' choices in an array then run a random function in a loop. 尝试将“ v”和“ c”选择存储在数组中,然后在循环中运行随机函数。 Let's say your j will be 'cvccv' 假设您的j为“ cvccv”

for i in range (0, 10):
    name = []
    for c in j:
        if sign == 'v':
             name.append(random.choice(vowels))
        if sign == 'c':
             name.append(random.choice(consonants))
    print(name)

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

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