Python Forum

Full Version: Something wrong with my code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Firstly I'm not a coder and at 80 I doubt whether I will ever have enough brain cells to attempt such a task.

But I do enjoy making Wordsearch Puzzle books. It keeps the little brain cells from going completely dead. The reason I'm here is that ChatGPT gave me a Python code

```python
import random

def generate_words(subject, word_length, num_words):
    words = []
    letters = 'abcdefghijklmnopqrstuvwxyz'
    for _ in range(num_words):
        word = ''.join(random.choice(letters) for _ in range(word_length))
        words.append(subject + word)
    return words

def save_words(words, folder_path):
    file_path = folder_path + '/generated_words.txt'
    with open(file_path, 'w') as file:
        for word in words:
            file.write(word + '\n')
    print(f"Words saved successfully to '{file_path}'.")

def main():
    subject = input("Enter the subject of the words: ")
    word_length = int(input("Enter the number of characters per word: "))
    num_words = int(input("Enter the number of words to generate: "))
    folder_path = input("Enter the folder path to save the file (e.g., C:/Users/Username/Desktop/WordGenerator): ")

    words = generate_words(subject, word_length, num_words)
    save_words(words, folder_path)

if __name__ == '__main__':
    main()
```
for a Word Generator that I could run on Windows but there is something wrong with the code. I ran it in Thonny and it gave this.....

SyntaxError: invalid syntax
Word Generator.py line 1

But there's nothing on line 1, so can anyone tell me what's gone wrong, please remember that I have hardly any knowledge about coding.

Thanks a lot
Three quotes is the start/end of a block string. Your entire program is one string. Essentially the entire program is "line 1". Remove the '''Python at the start and ''' at the end and try running. Don't be surprised when the words are all gibberish.
You were right it does spew out Gibberish. Is there a way to get it to spew out recognisable words in English instead?

Is there any Coder on this forum that would undertake an update of this code to make it into a decent piece of software?

I would pay for the person's time of course.

I hope I can ask that question as I'm in the doghouse with the moderators for not putting my code in quotes. But hey, it's my first post and bound to make mistakes. Will try harder in future.

Thanks a lot.

Michael.
You'll need to give CHATGPT a much better description of the requirements. Probably with enough detail that writing the code may be simpler than describing the code.

Maybe you can find a tool online that does what you want.

If you want to hire somebody to write this code, there is a Jobs section in the forum.

https://python-forum.io/forum-6.html

If you use proper tags, you don't have to stay in the dog house.
(Jul-03-2023, 04:21 PM)FabianPruitt Wrote: [ -> ]You were right it does spew out Gibberish. Is there a way to get it to spew out recognisable words in English instead?

Is there any Coder on this forum that would undertake an update of this code to make it into a decent piece of software?

I would pay for the person's time of course.
It's a task that's not so easy when talking about making recognisable words in English.
There are modules like eg random-word that will generate a working random English words.
Another way is to eg use chatGPT API trough Python,seach web.

For fun can use random-word module json database and mix it with code you posted.
This just save a random word,of course adding something like subject will be difficult.
word_length, num_words is possible to add without to much difficulty.
(Jul-03-2023, 04:21 PM)FabianPruitt Wrote: [ -> ]Firstly I'm not a coder and at 80 I doubt whether I will ever have enough brain cells to attempt such a task.
Cool,but should start with some easier task than this.
import random
import json

def generate_words(word_length=4):
    with open('words.json') as fp:
        result = json.load(fp)
        word, _ = random.choice(list(result.items()))
    return word

def save_words(word, folder_path):
    file_path = folder_path + '/generated_words.txt'
    with open(file_path, 'w') as file:
        file.write(word)
    print(f"Words saved successfully to '{file_path}'.")

def main():
    subject = input("Get a random words <Enter>: ")
    folder_path = input("Enter the folder path to save the file")
    word = generate_words()
    save_words(word, folder_path)

if __name__ == '__main__':
    main()
Output:
sauciness
Just a tip on getting words: gutenberg.org has a lot of books as text files. All these books are in the public domain, freely usable and copiable. Maybe they even have a dictionary!

Download a book.
Open the text file.

#! /usr/bin/python3
import random

path = '/home/pedro/temp/The_Knights_Tale_Middle_English.txt'

with open(path) as abook:
    # read the text to a list
    lines = abook.readlines()
    print('This list contains', len(lines), 'lines')
Using random you could select a random word from each line.

# now get a word from each line check if that word is in the list of words
# if not, add the word
words = []
for line in lines:
    # split the string line into individual words
    mylist = line.split()
    # choose a random word from the line
    word = random.choice(mylist)
    if not word in words:
        words.append(word)
Then you need to run through your words list and make everything uppercase. (Use: word.upper())
Put say 10 words or 15 words in the frame of your word-search puzzle, then fill in all other spaces with random letters!

Still quite bit to do! That should keep the old grey cells active!

Post what you have when it works!