在Python中,尝试次数的增量不会超过一次-请帮助?
每当我尝试几次来击败游戏时,它总是说猜测的次数是1,这不是真的。我做错了什么 我的代码:在Python中,尝试次数的增量不会超过一次-请帮助?,python,Python,每当我尝试几次来击败游戏时,它总是说猜测的次数是1,这不是真的。我做错了什么 我的代码: import random print("Welcome to Rock, Paper, Scissors. \nYou will be going up against the computer, who will randomly", "choose an object to duel you with!") user_win = False while not user_win:
import random
print("Welcome to Rock, Paper, Scissors. \nYou will be going up against the computer, who will randomly",
"choose an object to duel you with!")
user_win = False
while not user_win:
user_guess = input("\nChoose either Rock, Paper or Scissors: ")
user_guess = user_guess.lower()
if user_guess != "rock" and user_guess != "paper" and user_guess != "scissors":
print("You didn't enter a valid guess. Try again, please.")
user_guess = input("\nChoose either Rock, Paper or Scissors: ")
user_guess = user_guess.lower()
computer_guess = random.randint(1,3)
if computer_guess == 1:
computer_guess = "rock"
elif computer_guess == 2:
computer_guess = "paper"
else:
computer_guess = "scissors"
print("Your guess:", user_guess.capitalize(), "\nComputer guess:", computer_guess.capitalize())
number_of_guesses = 1
if user_guess == computer_guess:
print("\nThe game is a tie. You guessed", user_guess, "and so did the computer.")
number_of_guesses += 1
user_win = False
elif (user_guess == "rock" and computer_guess == "scissors") or (user_guess == "paper" and computer_guess == "rock"):
print("\nCongratulations! You have beaten the computer by playing", user_guess.capitalize(), "while the computer played", computer_guess.capitalize())
user_win = True
number_of_guesses += 1
else:
print("\nDamn! The computer won by playing", computer_guess.capitalize(), "while you played", user_guess.capitalize())
user_win = False
number_of_guesses += 1
if number_of_guesses == 1:
print("\nYou won, and it only took you %d try!" % number_of_guesses)
else:
print("\nYou won, and it only took you %d tries!" % number_of_guesses)
input("\nPress enter to exit the program.")
我认为格式正确。在这里输入代码并不容易。谢谢大家! 首先,在while循环中,每次运行时总是初始化
number\u of_guess=1
。这就是为什么每次运行时始终为1
在暂停之前进行此初始化 在循环中,您总是将猜测次数设置为1:
print("Your guess:", user_guess.capitalize(), "\nComputer guess:", computer_guess.capitalize())
number_of_guesses = 1 # setting here
在while循环之外设置
猜测次数
只要试着打印您正在更改的用户赢的地方
,您就会知道哪里出了问题。非常好。非常感谢。