Hello Team,

I've been having a wierd problem with 2 of my scripts and I now know the problem was in my if statements.

    if code == 949:
    elif code == 947:
    print servId

I run this script as:

sudo python printserv.py 949 254727 mesag

My result should be 62000001760, but somehow it always prints the value of else.

but if I do

if code in '949': ...

It always prints the correct thing. Someone please explain why this is so.

The items sys.argv are strings. So if the user gives 949 as the argument, code will be the string '949', not the number 949. Since == does not automatically convert between different types, 949 == '949' will be false -- only '949' == '949' would be true. So if you change it to code == '949', it will work.

You should not use code in '949'. That will not do what you want. in checks whether the left string is a substring of the right string. So if code would be '9' for example, code in '949' would be true.

What sepp2k says is correct, but I suspect that there is more to this that may be relevant. What is the script supposed to do, and how many different codes are there? If there are more than two or three different ones, you may want to use a dictionary to hold the possible key-value pairs, and do a lookup rather than having an extended if/elif/else block. For example:

from sys import exit, argv

if len(argv) < 4:
   print("You must provide exactly three arguments for this program.")

code = argv[1]
phone = argv[2]
message = argv[3]

lookup = {'949': '62000001760', '947': '62000001840'}

    servId = lookup[code]
except KeyError:

print servId