Python随机数猜谜游戏(无法正确猜测平均数)



我是新接触Python的,但现在我目前被我的这段代码困住了,我只是无法让每个游戏的平均猜测游戏工作。如果我能从你们那里得到一些帮助,那就太好了!如果有的话,我也很乐意听到任何建设性的反馈。谢谢!!

# A robust number-guessing game with hinting.
import random
total_guess = 0
num_games = 0
num_guesses = 0 
print("Welcome to Guessing Game!")
print()
reply = input("Are you ready to play (Y or N)?").upper()
while reply != "Y" and reply != "N":   
print("Invalid response")
reply = input("Another game (Y or N)? ").upper()
while reply == "Y": 
# pick a random number from 0 to 100 (both inclusive)
# There is no error in the line below, which is 
# equivalent to 
# number = random.randint(0, 100)
number = random.randrange(0, 101)
# make it different from number so that it goes into loop
guess = number + 1 
while guess != number :
guess = int(input("Your guess (0 - 100)? "))
num_guesses += 1
if guess < number :
print("Your guess is too low")
elif guess > number :
print("Your guess is too high")
else:
print("Bingo!")        
# collect the statistics to calculate average guess per game
total_guess += num_guesses
num_games += 1
print("You got it right in " + str(num_guesses) + " tries.")
num_guesses = 0
print()
reply = input("Another game (Y or N)? ").upper()
while reply != "Y" and reply != "N":   
print("Invalid response")
reply = input("Another game (Y or N)? ").upper()
print("Your average guess per game is", total_guess/num_games)

您的代码未正确缩进。

import random
total_guess = 0
num_games = 0
num_guesses = 0 
print("Welcome to Guessing Game!")
print()
reply = input("Are you ready to play (Y or N)?").upper()
while reply != "Y" and reply != "N":   
print("Invalid response")
reply = input("Another game (Y or N)? ").upper()
while reply == "Y": 
number = random.randrange(0, 11)
# make it different from number so that it goes into loop
guess = number + 1 
while guess != number :
guess = int(input("Your guess (0 - 100)? "))
num_guesses += 1
if guess < number :
print("Your guess is too low")
elif guess > number :
print("Your guess is too high")
else:
print("Bingo!")        
# collect the statistics to calculate average guess per game
total_guess += num_guesses
num_games += 1
print("You got it right in " + str(num_guesses) + " tries.")
num_guesses = 0
print()
reply = input("Another game (Y or N)? ").upper()
while reply != "Y" and reply != "N":   
print("Invalid response")
reply = input("Another game (Y or N)? ").upper()
print("Your average guess per game is", total_guess/num_games)

最新更新