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 trialLindsey Mote
Data Analysis Techdegree Student 1,148 PointsHelp with Error in Guessing Game
Help with Error in Guessing Game
I'm working on the number guessing game. I know I have quite a ways to go, but I'm hitting an error and struggling with how to resolve it. Here's my code:
# start_game():
import random, time, statistics
from random import randint
from statistics import median, mode, mean
guess_list = []
def start_game():
welcome()
# def num_of_guesses
### i don't know what to put here
# Display welcome message to the player.
print("Welcome to Lindsey's Amazing Guessing Game!")
time.sleep(1)
play_game = input("Would you like to play? Yes/No ")
while play_game:
# Store a random number as the solution
# code from https://www.geeksforgeeks.org/python-random-module/
def game_solution():
game_solution = int(random.randint(1, 20))
# Continuously prompt the player for a guess
new_guess = input("I've chosen a number between 1 and 20. What number do you think I picked? ")
new_guess = int(new_guess)
#if guess is greater than the solution, display "it's too low"
# save attempt number to a list
if new_guess > game_solution:
# append guess to guess_list
print("No, too low.")
num_of_guesses += 1
input("Guess again: ".format(new_guess))
#if guess is less than the solution, display "it's too high"
# save attempt number to a list
elif new_guess < game_soltuion:
# append guess to guess_list
num_of_guesses += 1
## don't forget you have to add the acutal guess too!!
print("No, too high.")
input("Guess again: ".format(new_guess))
guess_list.append(guess_list)
#if guess is correct, display player's name and "got it"
# --> pretty sure this isn't the right way to do it
while new_guess == game_solution:
print("You guessed it!")
break
# ask if they want to play again
play_again = input("Would you like to play again? yes/no ")
#if play_again == yes
#return to beginning
#else
# display number of guesses made to get the answer
# print("Thanks for playing! You made {} guesses.".format(len(num_of_guesses))
# at the end of the game, display 1) their number of attempts, 2) the mean, median, and mode of the saved attempts list
median_guess = median(num_of_guesses)
print(f"The median guess was: {median_guess}")
mean_guess = mean(num_of_guesses)
print(f"The mean guess was: {mean_guess}")
mode_guess = mode(num_of_guesses)
print(f"The mode guess was: {mode_guess}")
# Kick off the program by calling the start_game function.
The error I'm getting is after I guess a number:
if new_guess > game_solution: TypeError: '>' not supported between instances of 'int' and 'string'.
I think I have them both defined as integers, which is why this doesn't make sense.
TIA!
1 Answer
Steven Parker
231,269 PointsI'm surprised you get an error message about a string, I would expect to see a similar one about a function.
On line 25, game_solution
is defined as a function. My guess is you intended to perform the next line directly at that point (which would create it as an int) instead of defining a function.