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")
import random
wordlist = ['word1', 'word2', 'word3']
i = random.randint(0, len(wordlist)-1)
word = wordlist[i]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With