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

confused why two similar things don't produce the same result.

      response = str(input("double down? Y or N"))
      if response == str.lower(y):
          game()
      else:
          quit()

So, I'm making the python guess game right now, and I found out how to get the users response to continue the game as intended. but I want to know WHY things do or dont work.

the code above did NOT work. when the user input a [y] , I would get a 'NameError: y is not defined'. however, if I change the

 if response == str.lower(y):

TO THIS....

if response == str.lower('y'):

it works as intended

why does the first one not work, since I'm making it a string with 'str', and the second one works, although the 'y' should already be a string whether or not I put quotes around it?

I probably went overboard trying to clarify what my question actually was, but thanks for your time.

1 Answer

if response == str.lower(y): - Here y is a variable because it's not wrapped in quotes.

if response == str.lower('y'): - Here y is a string because it's wrapped in quotes

doesn't the str() function make it a string? I guess that's one of the basics I didn't fully understand.

str(2)  # == "2"

By doing str(y) you would just turn what ever stored in the variable y into a string, but it would not be == "y"

I hope this make sense? :-)

Ah yes. that makes perfect sense. no quotes makes python search for a variable named (whatever). thanks for the help!