Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hangman Game: One word is given, how can I give it a list

Tags:

python

I'm currently creating a Hangman game in Python. In the code, the word to be guessed is written. How can I change that to a list, and have a random word be chosen?

Here is my code:

print("What's your guess?")

word = "word"
guesses = ''

turns = 10

while turns > 0:
    failed = 0
    for char in word:
        if char in guesses:
            print(char)
        else:
            print("_")
            failed = failed + 1
    if failed == 0:
        print("You won")

        break

    guess = input("guess a character:")
    guesses = guess + guesses

    if guess not in word:

        turns = turns - 1

        print("Wrong")

        print("You have", + turns, "more guesses")

        if turns == 0:
            print("You lost, sorry")
like image 869
Kaise Avatar asked Jan 20 '26 03:01

Kaise


1 Answers

import random
wordlist = ['word1', 'word2', 'word3']
i = random.randint(0, len(wordlist)-1)
word = wordlist[i]
like image 125
G. B. Avatar answered Jan 22 '26 17:01

G. B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!