Reputation: 21
The propolem is Exception has occurred: IndexError
string index out of range. this happens in line 46
and to have lots of word option made a list with 5 letter words
i think the propolem is with the fact i origonial made it for number instead of leaters
The propolem is Exception has occurred: IndexError
string index out of range. this happens in line 46
and to have lots of word option made a list with 5 letter words
i think the propolem is with the fact i origonial made it for number instead of leaters
##
# wordel
# Print the welcome message.
print("Welcome to wordel!")
print("You will guess the final word.")
##
# random words
# using randint()
import random
# open file
with open(r"C:\Users\Admin\OneDrive\Documents\wordel list.txt") as file:
data = file.read()
words = data.split()
# Generating a random number for word position
final_word = random.randint(0, len(words)-1)
print("Position:", final_word)
print("Word at position:", words[final_word])
# Set an empty final_word.
final_word = ""
password_is_valid = False
# Print 100 empty lines.
for i in range(0, 100):
print()
# Keep track of the player's progress.
counter = 0 # The current turn number.
guessed_correctly = False # Whether Player 2 has guessed correctly.
all_guess_symbols = "" # All of the symbols.
# Loop 10 times, maximum
while counter < 10:
# Ask for player 2's guess.
guess_number = counter + 1
guess = input("guess the final_word: ")
# Only consider the guess if it is four digits long.
if len(guess) == 5:
# Create a string with the symbols for the current guess.
current_guess_symbols = ""
for i in range(0, 5):
if guess[0] == final_word[0]:
current_guess_symbols = current_guess_symbols + "🟢"
else:
current_guess_symbols = current_guess_symbols + "🟥"
# Add the current guess' shapes to the whole game's shapes.
all_guess_symbols = all_guess_symbols + current_guess_symbols + "\n"
print(current_guess_symbols)
# If the guess symbols are all green circles, Player 2 won.
# Set the
if current_guess_symbols == "🟢🟢🟢🟢":
# Set the counter to 10 to immediately end the game loop.
counter = 10
guessed_correctly = True
else:
# Continue to the next guess.
counter = counter + 1
else:
# Print an error message if the guess is too short.
print("Sorry, your guess is too short. Enter a 5 digit final_word.")
# Print all guess symbols at the end of the game.
print()
print(all_guess_symbols)
print()
# Print the ending message.
if guessed_correctly == True:
print("Congratulations, you guessed the final_word!")
else:
print("Sorry, you did not guess in time.")
print("The final_word was: " + final_word)
Upvotes: 0
Views: 105
Reputation: 169
You are getting IndexError at this line:
if guess[0] == final_word[0]:
This is most likely because either guess
or final_word
is an empty string. You have applied no input validation to the user's guess, so that could easily become an empty string. (also that line should probably be if guess[i] == final_word[i]:
).
However with the code as you've written, final_word
is guaranteed to be an empty string.
Your code at lines 14-21 does successfully read a word from the specified file (assuming your file is non-empty), but then at line 23 you have:
# Set an empty final_word.
final_word = ""
Which resets the variable to the empty string.
Upvotes: 0