-2

I need help in adding content from a notepad file called "Words.txt" to my Tkinter hangman game. If someone could write me a line of code to add to add to the one below -

WORDS = (add code here)

What needs to happen is every time new word is chosen it should randomly choose one from the list of words in the text document...I wrote all the code for the program to work and do what I want it to do but its just this one bit I cannot achieve.

My txt document consists of just a list of pretty much every word in the English language to make the game bit more challenging. Any help would be greatly appreciated in advance.

  • 1
    Welcome to Stack Overflow! This site isn't about getting people to write code for you, but to help people fix problems with their own code. What have you already tried to make this work? – ChrisGPT was on strike Feb 05 '14 at 13:33
  • I have tried to write my own for well over 6-7 hours but couldn't achieve it... I'm rather new to Python so I required some help with this small but essential part of my code... – DwarfTosser Feb 05 '14 at 14:34

1 Answers1

0

This assumes that each word is on a separate line:

import random
with open("Words.txt") as f:
    content = f.readlines()
word = random.choice(content).strip()
TNT
  • 3,392
  • 1
  • 24
  • 27
  • Thank you very much, I just had to change the 'word = random.choice(content).strip()' to 'word = content' as it chose a random letter from the word but besides that your piece of code just saved me hours of headache :) – DwarfTosser Feb 05 '14 at 14:36
  • So please consider clicking on that ugly grey triangle that is located above that ugly 0 left of my answer (not the one below please). This will turn the triangle green and the 0 to a (from a pure mathematical perspective infinitely better) 1. Thank You. – TNT Feb 05 '14 at 16:51
  • I defiantly would do that but I cannot since I do not have 15 reputation :( but as soon as I do get 15 I will click on it even if the thread will be 2 months old XD – DwarfTosser Feb 06 '14 at 13:14