Home > Mobile >  What is wrong in my script in this Python 3.x program?
What is wrong in my script in this Python 3.x program?

Time:12-08

import random

word_list = ["elma", "armut","kalem"]
chosen_word = random.choice(word_list)

stages = ['''
   --- 
  |   |
  O   |
 /|\  |
 / \  |
      |
=========
''', '''
   --- 
  |   |
  O   |
 /|\  |
 /    |
      |
=========
''', '''
   --- 
  |   |
  O   |
 /|\  |
      |
      |
=========
''', '''
   --- 
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
   --- 
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
   --- 
  |   |
  O   |
      |
      |
      |
=========
''', '''
   --- 
  |   |
      |
      |
      |
      |
=========
''']


map_1 = []
count = 0
end = False
for x in range(len(chosen_word)):
    map_1.append("_")

if "_" not in map_1:
    end = True
    Print("YOU WON")
if count == 6 and "_" in map_1:
    print("YOU LOST")
    end = True

while end == False:
    guess = input("Guess a letter ").lower()
    count  = 1
    print(stages[- count])
    for letter in chosen_word:
        
        if guess == letter:
            a = chosen_word.index(letter)
            map_1[a] = guess
            print(map_1)
        else:
            continue



print(map_1)

it does not stop when you guess all the letters, it gives IndexError: list index out of range every time ı execute the program,I didn't write a part for what happens when you make a wrong guess now ı do the correct guess every time but still it ends like this.I just started the learn in this part ı am stuck ı don't know how to solve this.

CodePudding user response:

you need to move the conditions into the while loop, otherwise, the program doesn't skip the while loop (end always is False).

while end == False:
    guess = input("Guess a letter ").lower()
    count  = 1
    print(stages[- count])
    for letter in chosen_word:
        
        if guess == letter:
            a = chosen_word.index(letter)
            map_1[a] = guess
            print(map_1)
        else:
            continue

    //end for loop   
    if "_" not in map_1:
        end = True
        print("YOU WON")
    if count == 6 and "_" in map_1:
       print("YOU LOST")
       end = True

  • Related