0

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!

2 Answers 2

1

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. 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! 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!

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. For example, you wouldn't want 5/2 to come out to 2, you want it to be 2.5, a float!

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help! I didn't think about using the number of problems as the variable instead of correct number of guesses. This has really helped me understand how to do average. Also, I have been doing a lot of research into python because it's the current language I am learning and on the operators lists, the += is on that list. I used it because in a previous question I had, someone else told me that it's easier to use. Thanks again though (:
Here is the revised edition including your input and mine. I had to change a couple things because it was just a simple syntax here and there. All in all, the program works just like it's supposed to and I just wanted to thank you for the help.
0

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. 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.

1 Comment

Yes, I was having trouble getting the AVGcounter variable to effectively work, but I am still new at this and not everything makes sense. What you say does make sense though and I will use this knowledge to help me in the future. Thanks (:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.