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

tuple: two variables has equal tuples, but they are not equal

Here is my code:

a = (0, 0)
b = input("a ")

if b == a:
  print("well done")

so when I launch this script and put (0, 0) in input, "well done" is not printed. Does anybody know why? Thank's in advance.

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

As they say "looks" can be deceiving". In Python 3, the input() statement returns a string:

>>> a = (0, 0)
>>> b = input("> ")
> (0, 0)
>>> a == b
False
>>> type(a)
<class 'tuple'>
>>> type(b)
<class 'str'>

In Python 2, there was a distinction between input() and raw_input(). Python 2 input() was equivalent to eval(raw_input()) (docs)

In Python 3, input() is equivalent to the Python 2 raw_input(). You need to add the eval to get what you want:

>>> eval(b)
(0, 0)
>>> type(eval(b))
<class 'tuple'>
>>> a == eval(b)
True

Thank you very much, Chris.