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
Matthew Price
511 Pointsconfused 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
Henrik Christensen
Python Web Development Techdegree Student 38,322 Pointsif 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
Matthew Price
511 PointsMatthew Price
511 Pointsdoesn't the str() function make it a string? I guess that's one of the basics I didn't fully understand.
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsHenrik Christensen
Python Web Development Techdegree Student 38,322 Pointsstr(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? :-)
Matthew Price
511 PointsMatthew Price
511 PointsAh yes. that makes perfect sense. no quotes makes python search for a variable named (whatever). thanks for the help!