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

Python随机数猜测游戏(无法获得正确的平均猜测),python,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 = inp

我刚开始接触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)
#一款具有暗示功能的强大数字猜测游戏。
随机输入
总数=0
num_games=0
num_猜测=0
打印(“欢迎参加猜谜游戏!”)
打印()
reply=输入(“您准备好玩了吗(Y或N)?”)。upper()
一边回答!=“Y”并回答!=“N”:
打印(“无效响应”)
回复=输入(“另一个游戏(Y或N)?”)。上限()
而回复==“Y”:
#从0到100(包括0和100)中随机选取一个数字
#下面的行中没有错误,这是
#相当于
#number=random.randint(01100)
number=random.randrange(0,101)
#使其与数字不同,以便进入循环
猜测=数字+1
猜猜看!=编号:
猜测=int(输入(“你的猜测(0-100)?”)
num_猜测+=1
如果猜测<数字:
打印(“您的猜测太低”)
elif guess>数字:
打印(“您的猜测太高”)
其他:
打印(“宾果!”)
#收集统计数据以计算每场游戏的平均猜测
总猜测次数+=总猜测次数
num_games+=1
打印(“你在“+str(num_猜测)+”tries中做对了。”)
num_猜测=0
打印()
回复=输入(“另一个游戏(Y或N)?”)。上限()
一边回答!=“Y”并回答!=“N”:
打印(“无效响应”)
回复=输入(“另一个游戏(Y或N)?”)。上限()
打印(“您每场游戏的平均猜数为”,总猜数/游戏数)

您的代码没有正确缩进

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)
随机导入
总数=0
num_games=0
num_猜测=0
打印(“欢迎参加猜谜游戏!”)
打印()
reply=输入(“您准备好玩了吗(Y或N)?”)。upper()
一边回答!=“Y”并回答!=“N”:
打印(“无效响应”)
回复=输入(“另一个游戏(Y或N)?”)。上限()
而回复==“Y”:
number=random.randrange(0,11)
#使其与数字不同,以便进入循环
猜测=数字+1
猜猜看!=编号:
猜测=int(输入(“你的猜测(0-100)?”)
num_猜测+=1
如果猜测<数字:
打印(“您的猜测太低”)
elif guess>数字:
打印(“您的猜测太高”)
其他:
打印(“宾果!”)
#收集统计数据以计算每场游戏的平均猜测
总猜测次数+=总猜测次数
num_games+=1
打印(“你在“+str(num_猜测)+”tries中做对了。”)
num_猜测=0
打印()
回复=输入(“另一个游戏(Y或N)?”)。上限()
一边回答!=“Y”并回答!=“N”:
打印(“无效响应”)
回复=输入(“另一个游戏(Y或N)?”)。上限()
打印(“您每场游戏的平均猜数为”,总猜数/游戏数)

第一条建议:使用正确的缩进。请包括它现在的工作方式(错误消息?输出错误?)以及您希望它的工作方式。