Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Word generator
#1
Hello guys,
can anyone make a code for me?
I need a 4 letter word generator , the word mustn't have a meaning, but atleast 1 vowel.
Ty for your help.
Reply
#2
(Jul-02-2017, 02:58 PM)jack1234 Wrote: can anyone make a code for me?
no, we would not do it for you, but we are more than willing to help. Post your code in code tags and ask specific question(s) regarding problems/errors you have.
And because this looks a lot like homework, I'm moving the thread in the respective section of the forum.
Reply
#3
This isnt an homework and my python level is very low so i dont even know wherr to start
Reply
#4
EDIT: I misunderstood you question. The word don't need a meaning? They can be some chars + one or more vowel?
In this case you should take a look into the module random. Look for choice.
Then you should look into string.ascii_lowercase or for more entropy into string.ascii_letters.
There is more than one way. First you can generate a list with 4 random chars from ascii_lowercase and check if after generation a vowel is inside. If not, repeat it.

Don't read it, if you won't do it with real words.

I you use this code, your teacher kills you :-D
If you use this code in a project, the other programmers will kill you.

with open('wordlist.txt') as infile, open('filtered_words.txt', 'w') as outfile:
   outfile.write('\n'.join(word for word in infile.read().splitlines() if len(word) == 4 and any(map(lambda v: v in word, 'aeiou'))))
used resource: http://www.ef.com/english-resources/engl...000-words/

First you should learn, how to open files.
Then learn about loops.
Read what the in operator does.
You need the len function to get the len of an element.
The code above won't help you to understand. It's just a silly joke.

What you have to do:
  • create an empty list
  • load a word list
  • iterate over the word list with a for loop
  • check the condition if a word has then length of 4
  • check if a vowel is in the word (with a second loop inside the loop), or read more about any, map, lambda
  • learn about the break statement in a while loop, for loop
  • if both conditions are fulfilled, append the word to the list

I think you need 3 days to learn the basics.
The code above is not basic and even hard to understand.

Use the newest Python version.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Hi Jack,

We've all been in the position of starting from scratch.

The best advice that I can give you is to start with a simple program, which will teach you an understanding of how a python program is written and seeing it run.

If this is what you want to do, then let us know and we can show you some tutorials to help you get there.

If you are building a house, you cannot build the walls or the roof until you have made the foundations. I appreciate that it is annoying but it is the only way.

Good Luck

Bass

"The good thing about standards is that you have so many to choose from" Andy S. Tanenbaum
Reply
#6
(Jul-02-2017, 04:40 PM)DeaD_EyE Wrote: First you can generate a list with 4 random chars from ascii_lowercase and check if after generation a vowel is inside. If not, repeat it.
or simply generate one random vowel, then generate 3 more random from all letters and eventually shuffle the 4 random letters and join them
Reply
#7
Or generate four random characters, and if none of them is a vowel, randomly replace one with a random vowel.

Or generate a random binary number from 0 to 14, and replace each 1 with a random consonant and each 0 with a random vowel.

Or generate all 262,495 legal words and randomly choose one.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
For what do you need this function? As a password generator?
If you want to generate passwords, there is a funny comic: https://xkcd.com/936/
To use english regular words is often better.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#9
On topic: I'd suggest import random, use some sort of bool() and run it four times, if all are false, do again. For each true, pick a random num from the ascii range for vowels, for each false, pick a random num from the ascii range for consonants. Optionally, (for a little bit more randomness) put them into a list, and change the order in some way. Then, char() all the ascii values, and finally print([i]index value[/i],end=()) for each object? index? (I'm still new myself) in the list so that it outputs everything on 1 line. If I knew how to convert a list to a str without a loop, I'd do that and output the string instead. Think I've tried joining but my experimental code so far is horrendous, and I haven't had any luck with getting expected output.
DeaD_EyE, what's wrong with your sample code? Is it because you are opening a word list instead of generating a word?

Off topic: DeaD_EyE, the XKCD solution was exploited over four years ago. Daily Mail has a good article about it: http://www.dailymail.co.uk/sciencetech/a...-hour.html. Didn't want that to be a clickable link, but I'm not seeing a way to unlink it since unlink is greyed out.
   TL,DR: Combination attacks use custom and standard dictionaries to combine common strings for cracking longer passwords in under 30 minutes.
Reply
#10
(Jul-03-2017, 03:07 AM)tozqo Wrote: DeaD_EyE, what's wrong with your sample code? Is it because you are opening a word list instead of generating a word?

My code was the wrong answer and is not readable for beginners.

from functools import partial


def with_vowel(word, word_len):
   # hint: strip newline when comparing the size
   return len(word.strip()) == word_len and any(map(lambda vowel: vowel in word, 'aeiou'))


# trying to do it memory efficient and readable
def filter_text(infilename, outfilename, condition):
   with open(infilename) as infile, open(outfilename, 'w') as outfile:
       for word in infile:
           # for loop iterates over lines if the file was opened in text mode
           # word has a newline character inside
           if condition(word):
               outfile.write(word)


with_vowel_len4 = partial(with_vowel, word_len=4)
with_vowel_len5 = partial(with_vowel, word_len=5)
filter_text('wordlist.txt', 'filtered_words.txt', with_vowel_len4)
filter_text('wordlist.txt', 'filtered_words_len5.txt', with_vowel_len5)
But this was not the task. Additionally there are modules in pip for password generation if needed.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,421 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  No output in 'english word generator' DeGerlash 14 6,532 Jan-10-2018, 12:23 PM
Last Post: squenson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020