I am trying to create a hangman game but there is a problem. Whenever I run my code the random.choice(words_to_guess)
chooses a word and when I write the word it has chosen and it's the correct word, it does not stop. It keeps going in loops.
What can I do to make stop the loop?
import random
lives = 10
def welcome():
name = input("enter your name: ")
print("hello " + name)
print( name + " you will have this many(10) ")
def computer_role():
words_to_guess = ["salad", "mood"]
random.choice(words_to_guess)
return random.choice(words_to_guess)
computer = computer_role()
def game_correct():
user = input("enter a letter: ")
while user in computer:
print(user)
game_correct()
def game_wrong():
for user in computer_role():
cal = lives -1
return cal
print("wrong word" + user + "this is how many lives you have left" + str(lives))
welcome()
computer_role()
game_correct()
game_wrong()
I have tried to use break
under where game_correct()
is but there seems that dose not wan work as well. also I have tried exit()
but that does not also work.
If you guess a correct letter the while loop will run forever as it never changes the variable called user. Instead of using a while loop, you should be using an if.
def game_correct():
user = input("enter a letter: ")
if user in computer:
print("Correct guess")
Should point you in the right direction.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.