Python- Finding average -
i trying find average number of times user guesses number. user asked how many problems want , program give them many problems. having trouble recording amount of times guess , wrong , guess , right , finding average between two. have far
print("hello!") 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 {0} * {1} ?".format(randoma,randomb)) while answer != realanswer: = input("what's 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 solve? ") problems = int(numproblems) if problems in range(1,11): problems in range(problems): hooblah() else: print("average number of tries: {0}".format(hooblah,average)) else: print("please input value between 1 through 10!") main()
thanks!
so tried change little possible not cramp style. think this, average number of guesses needed correct answer total number of guesses divided number of correct guesses. because make sure user gets correct answer, number of correct guesses number of problems!
so each time run hooblah(), return number of guesses took correct answer. add outside loop, @ end of loop, divide number of guesses number of problems , you've got answer! also, don't think python supports '+=', may need change avgcounter+=1 avgcounter = avgcounter +1, totally may mistaken, switch between languages bunch!
one note cast numguesses float ( float(numguesses) ), make sure int data type doesn't truncate average. example, wouldn't want 5/2 come out 2, want 2.5, float!
hopefully 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 {0} * {1} ?".format(randoma,randomb)) while answer != realanswer: = input("what's 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 solve? ") numproblems = int(problemsstring) numguesses = 0 if numproblems in range(1,11): problem in range(numproblems): numguesses = numguesses + hooblah() print("average number of tries: " + str(float(numguesses)/numproblems) else: print("please input value between 1 through 10!") main()
Comments
Post a Comment