Reputation: 153
Below is an outline of what I am trying to built:
Run the game
The code is working only if the inputted letters are constant.
Let's say if the game word is "Rain", the code will work only if user inputs: "R", "A", "I", "N".
Code will not work if the inputted letters are jumbled, like, "A", R", "I", "N".
I believe it can be achieved through iteration using enumerate, but I am not sure how.
Here is my code:
import random
WORDS = "wordlist.txt"
"""Getting Length input from user and selecting random word from textfile"""
def get_word_length_attempt():
max_word_length = int(input("Provide max length of word [4-16]: "))
current_word = 0
word_processed = 0
with open(WORDS, 'r') as f:
for word in f:
if '(' in word or ')' in word:
continue
word = word.strip().lower()
if len(word) > max_word_length:
continue
if len(word) < 4:
continue
word_processed += 1
if random.randint(1, word_processed) == 1:
current_word = word
return current_word
"""Getting input of number of attempts player wants to have"""
def get_num_attepmts():
num_attempt = int(input("Provide number of attempts you want: "))
return num_attempt
"""Displaying word in *"""
def display_word_as_secret():
display_word = '*' * len(get_word_length_attempt())
print(display_word)
"""Getting hint letter from user input"""
def get_user_letter():
user_letter = input("Enter letter: ").lower()
if len(user_letter) != 1:
print("Please Enter single letter")
else:
return user_letter
"""Starting Game"""
def start_game():
game_word = get_word_length_attempt()
attempts_remaining = get_num_attepmts()
print('Your Game Word: ' + game_word)
print('Your Game Word: ' + '*'*len(game_word))
print('Attempts Remaining: ' + str(attempts_remaining))
guessed_word = []
while attempts_remaining > 0:
next_letter = get_user_letter()
if next_letter in game_word:
print('You guessed correct')
guessed_word.append(next_letter)
print('Your Game Word: ' + game_word)
print('Your Game Word: ' + '*' * len(game_word))
print('Attempts Remaining: ' + str(attempts_remaining))
correct_word = "".join(guessed_word)
print(guessed_word)
if correct_word == game_word:
print('you won')
break
else:
print('The letter in not in the game word')
attempts_remaining -= 1
print('Your Game Word: ' + game_word)
print('Your Game Word: ' + '*' * len(game_word))
print('Attempts Remaining: ' + str(attempts_remaining))
else:
print('no attempts left')
print('You Lost')
print('The Word is: ' + game_word)
start_game()
Upvotes: 3
Views: 307
Reputation: 1020
The problem you describe is in line guessed_word.append(next_letter)
In effect you append letters in the order they are given by the player. Which is fine if you just compare the set of letters and number of occurrences, for example by using counter
from collections
. Or you can figure out where each supplied letter stands in the subject word and reconstruct that from user input
Upvotes: 0
Reputation: 20640
You're constructing correct_word
from the guessed letters in the order they were entered by the user. A guessed string 'ARIN'
is not equal to 'RAIN'
.
Instead, you need to do a comparison that doesn't care about the order. The simplest fix would be to change
if correct_word == game_word:
to
if set(correct_word) == set(game_word):
because sets will be compared for their content regardless of the order. It will also cope better with repeated letters, for example 'letterbox'
will just be treated as the collection of letters {'b', 'e', 'l', 'o', 'r', 't', 'x'}
.
You might as well store the guessed letters as a set in the first place because it doesn't make sense to guess the same letter more than once anyway.
Upvotes: 4