Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMarcus Kirchner
Front End Web Development Techdegree Student 3,822 PointsLooping through mean, median & mode and to end the number guessing game
Hello, I am having a lot of trouble looping through my mean, median & mode for my number guessing game, as well as offering the user the chance to finish the game. The instructions include after once the guess is correct, stop looping, inform the user they "Got it" and show how many attempts it took them to get the correct number. The attempts must then be saved to a list. At the end of the game, show the player, 1) their number of attempts, 2) the mean, median, and mode of the saved attempts list. Then the player must be asked if they want to play again. Is anyone able to look at my code and help? Thanks!
1 Answer
jb30
44,806 PointsIn line 12, from statistics import median, mean
imports median
and mean
, but mode
is not imported.
In line 26, if guessed_number < 1 or guessed_number > 30:
the statement is false if the guessed number is between 11
and 30
.
In line 40, attempts.append(attempts)
, the variable attempts
is the number of guesses rather than a list. If you are trying to get a list of all the numbers guessed, you could set attempts = []
in line 19 and attempts.append(guessed_number)
after the if
block in lines 26 through 28.
In lines 46 through 48,
mean_attempts = mean(attempts)
mode_attempts = mode(attempts)
median_attempts = median(attempts)
the variable attempts
is not defined, since these lines are not part of start_game
. If attempts
is meant to be the list of how many tries it took to guess the number, you could set attempts = []
outside the start_game
function and before the while
loop, and move lines 46 through 49 after line 55 (where the game can first be played).
In line 49, print("Your average score is {}, your median value is {} and your mode value is {}.".format(mean_attempts, mode_attempts, median_attempts)
is missing a closing )
. The order of the median and the mode are swapped between the format string and the values. It might be easier to read using an f-string: print(f"Your average score is {mean_attempts}, your median value is {median_attempts} and your mode value is {mode_attempts}.")
In line 54, if user_decision == "yes":
the variable user_decision
was not previously defined. Try changing it to if choice == "yes":
.
In line 55, new_game = start_game()
sets new_game
to the number of tries it took to guess the number, but the value of new_game
is never used. You could append the value in new_game
to an array attempts
to calculate averages by attempts.append(new_game)
if attempts
is previously defined outside of start_game
.
In line 61, start_game()
starts a new game after having said goodbye in line 57.