-1

I'm having following code, basically:

list = ["gibberish", "1"]

Variable = input()

if Variable is "1":

So, if someone enters 1, they should get a response. This means for me, "1" is exchangeable with list[1] now, so I should get a response from following code:

list = ["gibberish", "1"]

Variable = input()

if Variable is list[1]:

But instead, the condition is not fulfilled, I do not get any response. Neither by using "list[1]" nor str(list[1]), which normally appears to me as utter nonsense, but I tried anyway out of desperation. What am I doing wrong? Is the whole list or "" wimey stuff playing tricks on me? I'm thankful for every answer.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
MsXler
  • 9
  • 2

1 Answers1

0

The short answer is that is is not a substitute for "is equal" (==). is checks that two references point to the same object, whereas == checks whether two objects have equal values.

In your case, the "1" in the list and the "1" input by the user are two different objects that have the same value.

For more information see Why does comparing strings in Python using either '==' or 'is' sometimes produce a different result?

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Ah! Now I understand the == many programmers are talking about. In this case, the solution is obvious, even for many of upcoming problems I might encounter. Thank you! – MsXler Sep 22 '15 at 17:38