Mathime
Mathime

Reputation: 1490

Why is my word generator not working?

I am trying to create a random word generator (mostly nonsensical words).

The point is for it to:

But for whatever reason after I enter both x and y the program does nothing.

I tried adding print(attempt) after attempt = random.choice(sUpper), and it just generated:

H
G
E

etc.

Here is the program in question:

import random
sUpper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
sLower = 'abcdefghijklmnopqrstuvwxyz'
vowels = 'aeiouAEIOU'
class Word:
    def __init__(self, length):
        self.length = length
    def build(self):
            while 1:
                attempt = random.choice(sUpper)
                a = 0
                while a <= (self.length-1):
                    attempt += random.choice(sLower)
                    a += 1
                for i in vowels:
                    if i in attempt:
                        word = attempt
                        break
            return word
while 1:
    x = int(input('Length: '))
    y = int(input('Number: '))
    z = Word(x)
    w = 1
    while w <= y:
        print(z.build())
        w += 1

Upvotes: -1

Views: 125

Answers (1)

Keiwan
Keiwan

Reputation: 8251

You can change your build function to this:

def build(self):
    running = True
    while running:
        attempt = random.choice(sUpper)
        a = 0
        while a <= (self.length-1):
            attempt += random.choice(sLower)
            a += 1
        for i in vowels:
            if i in attempt:
                word = attempt
                running = False
                break
    return word

Upvotes: 3

Related Questions