我创建了一个游戏,其中您(玩家1)必须猜测比玩家2(计算机)更高的数字。您有3个猜测。我的问题是当您“获胜”时,程序会循环播放消息,而不是询问“您是否想再次玩”,另外,当您剩下0个猜测时,还会继续询问“猜测数字”,并且numGuesses也会更新到-1,-2等。它应该真正通过if numGuesses == 0部分来运行。
import random
player2num = random.randrange(1,16)
numGuesses = 3
print("Welcome to 'Guess Smart'!. Your goal as Player 1 is to guess a higher number then Player
2. You have 3 guesses. Only choose numbers from the list. Good Luck!")
Numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
print(Numbers)
num1 = int(input("guess a number: "))
while numGuesses != 0:
while not num1 in Numbers:
print("please choose a number in the list")
num1 = int(input("guess a number: "))
while num1 in Numbers:
if num1 > player2num:
print("You win!")
numGuesses = 0
elif num1 < player2num:
print("your number is too low, guess again")
numGuesses = numGuesses - 1
print("you have this many guesses left: ",numGuesses)
num1 = int(input("guess a number: "))
if numGuesses == 0:
playAgain = input("do you want to play again? (answer yes or no): ")
if playAgain == "yes":
num1 = int(input("guess a number: "))
elif playAgain == "No":
print("end of game")