简体   繁体   English

Python-求平均值

[英]Python- Finding average

I am trying to find the average number of times a user guesses a number. 我试图找到用户猜一个数字的平均次数。 The user is asked how many problems they want to do and then the program will give them that many problems. 用户被问到他们想做什么问题,然后程序将给他们带来很多问题。 I am having trouble recording the amount of times they guess and get wrong and guess and get right and finding the average between the two. 我在记录他们猜错了和猜错做对的次数以及找到两者之间的平均值时遇到了麻烦。 This is what I have so far 这就是我到目前为止

print("Hello!")
from random import randint

def HOOBLAH():
    randomA = randint(0,12)
    randomB = randint(0,12)
    answer = 0
    CORRECTanswer = (randomA*randomB)
    REALanswer = (randomA*randomB)
    AVGcounter = 0
    AVGcorrect = 0
    AVERAGE = 0
    print("What is {0} * {1} ?".format(randomA,randomB))
    while answer != REALanswer:
        an = input("What's the answer? ")
        answer = int(an)
        if answer == CORRECTanswer:
            AVGcorrect+=1
            print("Correct!")
            AVERAGE+=((AVGcorrect+AVGcounter)/AVGcorrect)
        else:
            AVGcounter+=1
            if answer > CORRECTanswer:
                print("Too high!")
            else:
                print("Too low!")


def main():
    numPROBLEMS = input("How many problems would you like to solve? ")
    PROBLEMS = int(numPROBLEMS)
    if PROBLEMS in range(1,11):
        for PROBLEMS in range(PROBLEMS):
            HOOBLAH()
        else:

            print("Average number of tries: {0}".format(HOOBLAH,AVERAGE))

    else:
        print("Please input a value between 1 through 10!")
main()

Thanks! 谢谢!

So I tried to change as little as possible as to not cramp your style. 因此,我尝试了尽可能少的改变以免抽筋。 So think about it like this, the average number of guesses needed to get the correct answer is just the total number of guesses divided by the number of correct guesses. 因此,以这种方式思考,获得正确答案所需的平均猜测数就是总猜测数除以正确猜测数。 Because you make sure the user eventually gets the correct answer, the number of correct guesses will just be the number of problems! 因为您可以确保用户最终得到正确的答案,所以正确的猜测数量就是问题的数量!

So each time you run HOOBLAH(), return the number of guesses it took to get the correct answer. 因此,每次您运行HOOBLAH()时,都要返回获得正确答案所需的猜测次数。 Add all those up together outside the for loop, then at the end of the loop, divide the number of guesses by the number of problems and then you've got your answer! 在for循环外将所有这些加在一起,然后在循环结束时,将猜测的数目除以问题的数目,然后您就会得到答案! Also, I don't think python supports '+=', so you may need to change AVGcounter+=1 to AVGcounter = AVGcounter +1, but I totally may be mistaken, I switch between languages a bunch! 另外,我不认为python支持'+ =',因此您可能需要将AVGcounter + = 1更改为AVGcounter = AVGcounter +1,但是我完全可能会误会,我在多种语言之间切换!

One note is I cast numGuesses to a float ( float(numGuesses) ), that is to make sure the int data type doesn't truncate your average. 一个注意事项是我将numGuesses强制转换为浮点数(float(numGuesses)),以确保int数据类型不会截断平均值。 For example, you wouldn't want 5/2 to come out to 2, you want it to be 2.5, a float! 例如,您不希望5/2等于2,而您希望它等于2.5(浮点数)!

Hopefully that helps! 希望有帮助!

from random import randint

def HOOBLAH():
    randomA = randint(0,12)
    randomB = randint(0,12)
    answer = 0
    CORRECTanswer = (randomA*randomB)
    REALanswer = (randomA*randomB)
    AVGcounter = 0
    AVERAGE = 0
    print("What is {0} * {1} ?".format(randomA,randomB))
    while answer != REALanswer:
        an = input("What's the answer? ")
        answer = int(an)
        if answer == CORRECTanswer:
            print("Correct!")
            return AVGcounter
        else:
            AVGcounter+=1
            if answer > CORRECTanswer:
                print("Too high!")
            else:
                print("Too low!")


def main():
    problemsString = input("How many problems would you like to solve? ")
    numProblems = int(problemsString)
    numGuesses = 0
    if numProblems in range(1,11):
        for problem in range(numProblems):
            numGuesses = numGuesses + HOOBLAH()
        print("Average number of tries: " + str(float(numGuesses)/numProblems)

    else:
        print("Please input a value between 1 through 10!")
main()

I'm not totally sure what you're trying to do, but if you want to give the user x number of problems, and find the average number of guesses per problem, you'll want your HOOBLAH() function to return the number of guesses for each run. 我不确定您要做什么,但是如果您想给用户x个问题的数量,并找到每个问题的平均猜测数,您将希望HOOBLAH()函数返回该数字每次运行的猜测。 You can keep track of this outside your method call and average it at the end. 您可以在方法调用之外对此进行跟踪,并在最后进行平均。 But right now, your program has no access to AVGcounter, which seems to be the variable you're using to count the number of guesses, outside the HOOBLAH() function call. 但是现在,您的程序无法访问AVGcounter,这似乎是您用来在HOOBLAH()函数调用之外计算猜测数量的变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM