Hangman Starter Game: Can't Make It Work

I just started coding (in Python 3) the day before yesterday (which makes me a SERIOUS beginner), and I realized that I would try to make my own Hangman game, but I just don’t know what happened to what I have done so far! ^ _ ^ This is it:

L = ["cat", "dog", "rabbit"]
from random import randrange
random_index = randrange(0,len(L))
w = L[random_index]
W = list(w)
a = input()
tries = 1
print(W)
while len(W) != 0 and tries<10:
    if a in W:
        print("yes")
        W.remove(a)
        tries += 1
        a = input()
    elif a not in W:
        print("no") 
        tries += 1
        a = input()
else:
    if len(W) == 0: 
        print("Well done! Your word was")
        print(w) 
    elif tries == 10:
        print("You died!")

I think the problem comes from my thingy loop, and len (W)! = 0 ", because everything is fine with the input part, it just does not stop when it is needed! (that is, when it is assumed that there is nothing left to guess!) Therefore, I hope that someone will be good enough to spend two minutes of their day to help me with my main, not very interesting problem! Thanks in advance!

+4
source share
3
  • random.choice(L) , L[random.randrange(len(L))]

from random import choice

def show_word(target_word, remaining_letters, blank="-"):
    print("".join(blank if ch in remaining_letters else ch.upper() for ch in target_word))

words = ["cat", "dog", "rabbit"]
target_word = choice(words)
remaining_letters = set(target_word)

print("Let play Hangman!")

for round in range(1, 11):
    show_word(target_word, remaining_letters)
    guess = input("Guess a letter: ").strip().lower()
    if guess in remaining_letters:
        print("You got one!")
        remaining_letters.remove(guess)
        if not remaining_letters:
            break
    else:
        print("Sorry, none of those...")

if remaining_letters:
    print("You died!")
else:
    print("You solved {}! Well done!".format(target_word.upper()))
+1

, , . -

L = ["cat", "dog", "rabbit"]
from random import randrange
random_index = randrange(0,len(L))
w = L[random_index]
W = list(w)
tries = 0
print(W)
while len(W) != 0 and tries<10:
    a = input()
    if a in W:
        print("yes")
        W.remove(a)
        tries += 1
    elif a not in W:
        print("no") 
        tries += 1
else:
    if len(W) == 0: 
        print("Well done! Your word was")
        print(w) 
    elif tries == 10:
        print("You died!")
0

If I understand your problem well, you can solve this problem like this, also remember that you can provide information to the user adding a prompt in the input method:

L = ["cat", "dog", "rabbit"]
from random import randrange
random_index = randrange(0,len(L))
w = L[random_index]
W = list(w)
tries = 1
print(W)
while len(W) != 0 and tries<10:
  a = input("select word")
  if a in W:
    print("yes")
    W.remove(a)
    tries += 1
  else:
    print("no") 
    tries += 1

  if len(W) == 0: 
    print("Well done! Your word was")
    print(w)
  elif tries == 10:
    print("You died!")
0
source

Source: https://habr.com/ru/post/1679827/


All Articles