Andrew
Andrew

Reputation: 39

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?

Upvotes: 0

Views: 15439

Answers (4)

maddie
maddie

Reputation: 1

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)

Upvotes: 0

FernAndr
FernAndr

Reputation: 1608

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

Upvotes: 2

Abdullah Razzaki
Abdullah Razzaki

Reputation: 992

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

Upvotes: 0

akp
akp

Reputation: 637

Because you are joining the same j list 10 times in the loop:

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

Upvotes: 0

Related Questions