简体   繁体   中英

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.

Expected output: For the input, I want to generate the generated text j for 10 times each one with different texts. For the example input, the expected output should be - sdfes gdadf nkadj like this 10 words.

Output yielded: Instead of getting 10 different texts, I am getting an output like sdfes sdfes sdfes - the same text for 10 times.

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:

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. 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:

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

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. Try to store 'v' and 'c' choices in an array then run a random function in a loop. Let's say your j will be '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)

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