首页 > 解决方案 > 虽然猜谜游戏中的函数不断循环,python

问题描述

我是 Python 的初学者,我想知道为什么它一直在循环,有人可以帮助我。当我打印出来时,即使我猜对了数字,它也会保持循环

import random

answer = random.randint(1,10)
lives = 5
out_of_lives = 0

print("Hi there! What is your name? ")
name = input()

print("Alright " + name + ", we're playing guess the number between 0 and 20.")

guess = int(input("Take a guess: "))
while (lives != 0) or (guess != answer):
    if guess > answer:
        print("Guess lower!")
        lives = lives - 1
        print("You have got", lives, "lives left")
        guess = int(input('Try again, your new guess: '))
    elif guess < answer:
        print("Guess higher")
        lives = lives - 1
        print("You have got", lives , "lives left")
        guess = int(input('Try again, your new guess: '))
    elif guess == answer:
        print("Good job, you guessed the number")
    elif lives == 0:
        print("Game over!, I was thinking about the number: ", answer)

标签: pythonwhile-loop

解决方案


是的,这是因为您还需要在您希望它停止的地方打破循环。


推荐阅读