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

.lower Not Working

I was making a number game and wanted to lowercase an input and then see if it equaled y or n, but it didn't work. Instead, it just crashed. Here's my code:

import random
import time

sn = random.randint(1, 100)
ga = []

def game():
    global sn
    global pa
    global ga
    print("You have 15 guesses to figure out my number!")
    while len(ga) < 15:
        g = input("""Guess a number between 1 and 100:
""")
        try:
            g = int(g)
        except ValueError:
            print("{} is not a number!".format(g))
            continue
        if g == sn:
            pa = str(input("""You guessed my number! My number was {}. Play again? Y/N:
""".format(sn)))
            if pa.lower() == "y":
                sn = random.randint(1, 100)
                ga = []
                continue
            elif pa.lower() == "n":
                print("Thanks for playing! Hope to see you again!")
                time.sleep(4)
                break
            else:
                print("I'll take that as a no. Thanks for playing anyway!")
                break
        if g > sn:
            print("That's not my number! My number is smaller than that!")
        elif g < sn:
            print("That's not my number! My number is larger than that.")
        ga.append("a")
        continue
    while len(ga) == 15:
        gpa = input("""Looks like you're out of guesses! My number was {}. Would you like to try again? Y/N:
""".format(sn))
        if gpa.lower() == "y":
            sn = random.randint(1, 100)
            ga = []
            continue
        elif gpa.lower() == "n":
            print("Thanks for playing! Hope to see you again!")
            time.sleep(4)
            break
        else:
            print("I'll take that as a no. Thanks for playing anyway!")
            time.sleep(4)
            break
game()

Can somebody tell me why .lower isn't working for this?

  1. Run the code above. Guess 15 wrong numbers. Then it will ask you to play again. Type a capital Y and the game just breaks with no exception. In the code, it’s supposed to restart the loop.
  2. Python 3.x
Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Ah, so "crashed" was an overstatement. It ran as coded, but not as desired.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

The flow as you have it now:

  • upper while loop runs during initial game
  • if 15 guesses made, it falls into second while loop
  • second while loop either
    • resets ga on yes which falls out of the second while because len() is now 0, or
    • uses break statement to end second while loop
  • game ends because there is no code after the second while. Perhaps add a call to game()

Thank you! I forgot there was a second while loop.