Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Word Generator
It works like this:
1 Vowel, 1 Consonant, 1 Vowel, 1 Consonant.. (So It sounds like a word..) VCVCVC

Vowels: A, E, I, O, U
Consonants: B, C, D, F, G, H, J, K, L, M, N, P, Q, R, S, T, V, X, Z / W, Y, Q..

This is what I got so far:

import random
import string

VOWELS = list("AEIOU")
CONSONANTS = list(set(string.ascii_uppercase) - set(VOWELS))

def generate_word(length = 5):
    return "".join(random.choice([VOWELS, CONSONANTS][_ % 2]) for _ in range(length))

Now what's left is removing Q&Y&W out of consonants and make letters not repeat. Can somebody help me with this?
Since CONSONANTS is a list, you can remove the letters you want with the remove method (CONSONANTS.remove('Q')). To not double letters, I would turn the generator comprehension into a for loop that builds the string up character by character. Then have a while loop in the for loop, that picks random letters until it picks one that isn't the last character of the string so far.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

def generate_word(length = 5):
return "".join(random.choice([VOWELS, CONSONANTS][_ % 2]) for _ in range(length))

Please, only use underscore for a variable name if you're not going to use that variable at all. [_ % 2] looks like some Perl nonsense, and shouldn't be anywhere in your code.
Mekire likes this post

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  print a word after specific word search evilcode1 8 243 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  difference between word: and word[:] in for loop zowhair 2 946 Mar-03-2018, 07:24 AM
Last Post: zowhair
  receive from a generator, send to a generator Skaperen 9 1,323 Feb-05-2018, 06:26 AM
Last Post: Skaperen
  Python Speech recognition, word by word AceScottie 5 6,658 Aug-21-2017, 12:37 PM
Last Post: DeaD_EyE

Forum Jump:

Users browsing this thread: 1 Guest(s)