Python Forum
Word Game - Printable Version

+- Python Forum (
+-- Forum: Python Coding (
+--- Forum: General Coding Help (
+--- Thread: Word Game (/Thread-Word-Game)

Word Game - paulmerton4pope - Jul-10-2020

Hi everyone,

I hope you're all keeping safe during this time.

I'm trying to create a word game in Python (using Thonny), whereby the user has to come up with a word from a random sample of ten letters.

I have managed to get the code to match whereby, if letters not in the sample, are used, the user is reminded to use the ones in the sample.

But I'm finding that my "print" command is printing for each character used in the word, as opposed to just the word itself. It is also increasing the score three times instead of once.

Please see image below for reference.

import random
parhau = True

letters = []
for num in range (97,123):
    letters.append (chr(num))

x = 0
rlist = random.sample(letters,10)
print (rlist)
while parhau:
    word = str(input ("Please enter a word "))
    for ch in str(word):
        if not ch in rlist [1:11]:
            print ("Nay - try again!")
        if ch in rlist and len (word) >= 3:
            x += 1
            print ("Boom! Your score is now " + str(x + 1) )
I am a complete beginner with Python, so please do bear with me.

If you can help me, I would be very grateful.

Kind regards,

RE: Word Game - GOTO10 - Jul-10-2020

Hello paulmerton4pope,

The reason your print command is printing once for every letter is because it is included in your loop for ch in str(word):. If you remove a couple of indent levels (have your print line at the same level as your for loop above it), it will only print once after your loop is done.

There are a handful of other issues to note:
  1. From a style standpoint, it's preferable not to have spaces between function names and parentheses. print(something) is preferred over print (something}, and so on. You've done this correctly in some lines but not others.
  2. There are simpler ways to create a list of letters than looping through the chr() function as you do in lines 5 and 6. Also, keep in mind that you can iterate through a string just as easily as through a list, so you don't necessarily require a list at all.
  3. The input() function returns a string value by default, so you don't need to use str() in lines 12 and 13, since that value will already be a string.
  4. Your slice in line 14 means that the first letter in the list is not considered valid, since indexes begin at 0 and not at 1. You don't need to include any index values at all since you are considering your whole list, so you could just use rlist instead of rlist[1:11]. (Note that this is another place where it's preferable NOT to include a space.)
  5. You may want to rethink the logic of your scoring system. As it currently stands, a user still gets points for each valid letter, even if they include invalid letters in the word. You might want to start by evaluating whether there are any invalid letters. If so, tell the user and prompt for another word. Only loop through and give points for each letter after you've verified that all of them are valid.
  6. Unless you are meaning to add 1 to the score each time you print it, you should use just x instead of x + 1 on line 18.

Don't let any of these comments discourage you! You are off to a good start with what you've done so far.

RE: Word Game - menator01 - Jul-10-2020

Keep at it you're doing good.
I modified your code a little to get it to work.
As GOTO10 suggest, there are better ways.
Keep up the good work.

#! /usr/bin/env python3
import random

letters = []
for num in range (97,123):
    letters.append (chr(num))

score = 0

rlist = random.sample(letters, 10)


myword = []

while True:
        word = input('Enter a word: ')
        for ch in word:
            if ch in rlist:
        newword = ''.join(myword)
        myword = [] # Reset so it does not carryover
        if newword and len(newword) >= 3:
            score += 1
            print(f'Your word is {newword} and your score is {score}') # Print results if match
            newword = [] #R eset
            print('Nah, try again')
            newword = [] # Reset
    except ValueError as error:
['i', 'v', 'y', 'k', 'n', 'e', 'p', 'z', 'u', 'b'] Enter a word: puppy Your word is puppy and your score is 1 Enter a word: ralph Nah, try again Enter a word: pop Nah, try again Enter a word: puke Your word is puke and your score is 2

RE: Word Game - menator01 - Jul-10-2020

And one more

#! /usr/bin/env python3
import random
import string
from subprocess import call
import os

# Start score at zero
score = 0

# Create a empty list
myword = []

# Function to clear screen of cluttered text
def clear():
    _ = call('clear' if == 'posix' else 'cls')


# Start the while loop
while True:

    # Get 10 letters from the alphabet
    letters = random.sample(string.ascii_lowercase, 10)

    # Print letters to choose from
        print('\nType q and press enter to exit.\n')
        # Ask for a word
        word = input('Enter a word: ').lower()

         # A way to exit the game
        if word == 'q':

        # Compare our word with the letters list and creat a list of matched letters
        [myword.append(ch) for ch in word if ch in letters]

        # Join the letters in list to make word
        myword = ''.join(myword)

        # Check to see if myword has letters and there is at least 3
        if myword and len(myword) >= 3:

            # Increase score by 1
            score += 1

            # Clear screen and print message
            print(f'\nYour word is {myword}.')
            print(f'Boom! Your score is now {score}.')

            # Reset myword to empty list
            myword = []

            # Clear screen and print message and continue
            print(f'\nSorry, please try again.')

            # Reset myword to empty list and continue
            myword = []
    except ValueError as error:

RE: Word Game - paulmerton4pope - Jul-11-2020

Thank you both so much - that is really helpful.

As I say, I am a complete beginner, so it is all very new to me!