0
print('''do you wish to access this network''')
VAL= int(input("to entre please punch in the pass word: ")
if VAL is 2214 **:**
      print("welcome")
else:
      print("wrong password, please check retry")
Holger Just
  • 52,918
  • 14
  • 115
  • 123

4 Answers4

5

You forgot to close a parenthesis:

print('''do you wish to access this network''')
VAL= int(input("to entre please punch in the pass word: "))   # here!
if VAL is 2214:

I'd also suggest to avoid using the is operator if you want to compare for equality.

The is operator compares identities(i.e. it's the same object, in the same memory location), while == compares for equality(i.e. these objects, according to their semantics, can be considered equal).

Using is to test for equality works only on integers in the range [-5, 256] due to an implementation detail(i.e. these numbers are cached). It fails for all the other numbers.


To expand a bit about why the colon is highlighted, instead of the if itself:

Remember that in python you can enclose every expression between parenthesis, in order to write it in multiple lines, but you cannot put statements inside parenthesis. There is a clear distinction between statements and expressions.

Statements include loops statements(for, while, break, continue, else), the if-elif-else, try-except-finally, with, assignment name = value, definition of functions and classes etc.

Expressions are everything else: a + b, object.method(), function_call() ...

In your specific example the parser sees the line:

VAL= int(input("to entre please punch in the pass word: ")

which is an assignment statement. It assigns to VAL the value of the expression on the right. Thus it parses the expression int(input(...) ... and since there is no closing parenthesis on this line it continues parsing on the following line. But on the next line it finds:

if VAL is 2214:

This is a statement because there is the : colon at the end, and expressions cannot contain statements. This is also why you cannot do things like if (a=100) < 50:, i.e. assignment inside a condition.

The if VAL is 2214 itself is not an error because there exist also an if-expression(actually called conditional expression). For example the following is valid python code:

VAL = int(input("prompt ")
if n % 2 == 0 else input("different prompt "))

However in this case you must specify both if and else and there is no colon inside a conditional expression.

Community
  • 1
  • 1
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
2

The first mistake is that you forgot to close ) as @Bakuriu pointed out:

VAL= int(input("to entre please punch in the pass word: "))

Second mistake is that you use is for comparing numbers which is wrong way to compare numbers. is is used for identifying things like, a is None, or a is a. With numbers, it will only works for small numbers under 256:

>>> a = 10
>>> b = 10
>>>
>>> a is b
True
>>>
>>> a = 256
>>> b = 256
>>>
>>> a is b
True
>>>

but above this number it will returns False:

>>> a = 257
>>> b = 257
>>>
>>> a is b
False
>>>

You should always use == for comparing numbers.

  • 1
    I am hoping by his use of input instead of raw_input and the use of print as a function not as a statement that it is 3.x, in which case `(257 is 257)` is True, since 3.x drops the distinction between ints and longs, however even with that, == should be used for numeric comparisons unless you specifically want floats with integer values to not match with ints of the same value `1.0 is not 1`. – Perkins Sep 05 '13 at 07:55
  • 2
    Third mistake is not to use the [naming conventions from the Style Guide for Python Code](http://www.python.org/dev/peps/pep-0008/#naming-conventions). (VAL -> val) – Matthias Sep 05 '13 at 07:59
  • 1
    Oh, and one other note in regards to is vs ==, with python3, is completes on integer comparisons in roughly half the time compared to ==, so if you have multiply nested non-refactorable code performing some sort of linear search looking for a value, is works better than ==. – Perkins Sep 05 '13 at 08:05
  • 1
    One thing to note about PEP8 is that it is principally for the core python distribution itself, and third party frameworks designed as extensions (twisted, django, et cetera), so while I don't agree that all new projects should follow PEP8 to the letter, if you want your code to make it into the standard library, it needs to. Also, if you're going to deviate from PEP8, you should probably have a good reason to do so. – Perkins Sep 05 '13 at 08:10
  • `is` doesn't compares the *value* it compares if object `x` is `y`, for example `x = 10000; y = x; x is y`, which in this case `y` points to `x`. `257 is 257` didn't return `True` because they are *equal*, but because they are the same object, same `id()` number. Try it out: `id(257), id(257)`. –  Sep 05 '13 at 08:19
  • 1
    @Perkins `257 is 257` works in *any* python version because the compiler will [optimize the bytecode using a single constant](http://stackoverflow.com/questions/15171695/weird-integer-cache-inside-python-2-6/15172182#15172182). If you enter `a = 257` and `b = 257` in *two different lines* in the interpreter then `a is b` is `False`, while using `255` returns `True`. Also it is *not* true that `is` can be used to speed-up linear search. You can use it only if you are *certain* that you have a reference to the *exact object* contained in the sequence. In the 99% of the cases this is *not* true. – Bakuriu Sep 05 '13 at 11:11
  • 1
    Hm, you're right, guess I never tried that when I was testing with it. – Perkins Sep 06 '13 at 03:58
0

Make sure to close both parentheses on this line.

VAL= int(input("to entre please punch in the pass word: ")
Tyler Fox
  • 170
  • 1
  • 12
0

Looks like you're trying for python 3.x, I don't know why you are ending a line with :, it should just end with : Like this.

print('''do you wish to access this network''')
VAL= int(input("to entre please punch in the pass word: "))
if VAL is 2214:
      print("welcome")
else:
      print("wrong password, please check retry")

note the change to line 3. Also note the missing paren on line 2 Also, consider doing a comparison by string instead of an int conversion, since if the user enters a non-digit, it will raise an Exception to convert it to int.

I would use the following.

VAL=input("to enter please punch in the password: ")
if VAL=='2214':
  print('welcome')
else:
  print("wrong password, please retry")

As a final note, the above code is dangerous to run on python 2.x, since input executes the entered string instead of capturing the string value.

Perkins
  • 2,409
  • 25
  • 23
  • Also, unless you need `VAL` again later, just use `if input('txt')=='2214':`, and if you want it to ignore whitespace, make it `input('txt').strip()` – Perkins Sep 05 '13 at 08:00