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 trial

Python

Can someone help me understand the best way to have an input formatted as an integer, but then assessed in a str?

Essentially I need to have the user input a "guess" for a game. I need to take that variable and compare it to a string, to quit of the user enters "quit", but I also need it to be an integer to be evaluated for other conditions

""" hi_score = []

def start_game(): answer = random.randint(1,10) num_of_guess = 0

while answer > 0:
    try:
        guess= int(input("Pick an integer between 1 & 10:  "))
        num_of_guess += 1

    except ValueError as err:
        print(("Whoops, not a valid entry.Please try again using an Integer"))
        continue


    if str(guess.lower()) == "quit":
        print("Thanks for playing Rando! Come back anytime!")
        exit()

    elif 1<= guess <= 10:

        if guess == answer:
            print("Congrats! You guessed my number. It took you {} tries".format(num_of_guess))
            replay = input('Would you like to play again?  ')

            if replay.lower()== "yes":
                current_hi_score(num_of_guess)
                start_game()

            else:
                print("Thanks for playing, comeback again!")
                exit()

        else:
            if guess > answer:
                print("It's lower")
            else:
                print("It's higher")

    else:
        print("Number has to be integer between 1 & 10, try again!")

def current_hi_score(num_of_guess): hi_score.append(num_of_guess) print('Score to beat is {}! Good Luck '.format(min(hi_score)))

start_game()

"""

1 Answer

Hey Jamar Slade !

Try converting your guess variable to a string first, then using the .lower() method on it, like this:

str(guess).lower()

.lower() only works on strings and the way it was written before with guess.lower() was throwing an error because it was trying to put an integer in lowercase. :D

Hope that helps!

Mel,

thanks for the response, I've changed my script to reflect str(guess).lower() as you mentioned above. More than anything though even with that when I run my program, and I input quit, it gives me my try/except error message. This wasn't part of the assignment, but I wanted to do it just for my own knowledge. I believe it's happening because the "quit line" is run after the except ValueError script and that's taking any non-numeric value and feeding it to the try/except block. When I try to move the quit line above the try/except block it tells me that "guess" has not been defined yet.

'''

import random GREETING = "======> Welcome to Rando <====="

print(GREETING.upper())

rules = print("Rules: \n***Select a integer between 1 & 10, and I will tell you if it's too high or too low.\nIf at any point you want to quit type 'Stop'***")

def start_game(): answer = random.randint(1,10) num_of_guess = 0

while answer > 0:
    try:
        guess= int(input("Pick an integer between 1 & 10:  "))
        num_of_guess += 1

    except ValueError as err:
        print(("Whoops, not a valid entry.Please try again using an Integer"))
        continue


    if str(guess).lower() == quit:
        print("Thanks for playing Rando! Come back anytime!")
        exit()

    elif 1<= guess <= 10:

        if guess == answer:
            print("Congrats! You guessed my number. It took you {} tries".format(num_of_guess))
            replay = input('Would you like to play again?  ')

            if replay.lower()== "yes":
                current_hi_score(num_of_guess)
                start_game()

            else:
                print("Thanks for playing, comeback again!")
                exit()

        else:
            if guess > answer:
                print("It's lower")
            else:
                print("It's higher")

    else:
        print("Number has to be an integer between 1 & 10, try again!")

def current_hi_score(num_of_guess): hi_score.append(num_of_guess) print('Score to beat is {}! Good Luck '.format(min(hi_score)))

start_game()

'''