Word Game - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Word Game (/thread-28230.html) |
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, paulmerton4pope 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:
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) print(rlist) myword = [] while True: try: word = input('Enter a word: ') for ch in word: if ch in rlist: myword.append(ch) else: pass 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 else: print('Nah, try again') newword = [] # Reset except ValueError as error: print(error)
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 os.name == 'posix' else 'cls') clear() # 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(f'\n{letters}') try: 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': break # 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 clear() print(f'\nYour word is {myword}.') print(f'Boom! Your score is now {score}.') # Reset myword to empty list myword = [] else: # Clear screen and print message and continue clear() print(f'\nSorry, please try again.') # Reset myword to empty list and continue myword = [] continue except ValueError as error: print(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! |