Python Forum
Something wrong with my code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Something wrong with my code
#1
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
deanhystad write Jul-03-2023, 02:43 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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.
Reply
#3
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.
Reply
#4
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.
Reply
#5
(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
Reply
#6
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 494 Nov-07-2023, 04:32 PM
Last Post: snippsat
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,588 Mar-27-2023, 07:38 AM
Last Post: buran
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 1,269 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,822 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,584 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Wrong code in Python exercise MaartenRo 2 1,542 Jan-01-2022, 04:12 PM
Last Post: MaartenRo
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,637 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  VS Code debugger using wrong Python environment topfox 0 2,520 Jun-09-2021, 10:01 AM
Last Post: topfox
  What is wrong with my code??? MrLeads 15 5,100 Sep-16-2020, 02:00 PM
Last Post: MrLeads
  Can somebody check what is wrong with my code? hplus_liberation 4 2,624 Sep-16-2020, 05:52 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

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