1

I have an issue with my code.

def function_test(valid_employee,employee_number):
    print """ valid_employee : %d \n employee_number: %d"""  
        %(valid_employee, employee_number)

    if employee_number == valid_employee:   # my if statement seems to be wrong:
        print " EMPLOYEE NUMBER RECOGNISED, WELCOME %d " %(employee_number)
    else:
        print "EMPLOYEE NOT RECOGNISED"

print "Please enter your employee_number "
employee_number = int (raw_input())
test_function = function_test(030532,employee_number)

I'm trying to pass 030532 (the valid_employee) but everytime time I print it, in the function(function_test) I get the value '12634' when it should be '030532'. I am using an if statement to check both employee_number and valid_employee within the function to confirm if the employee is valid.. that being valid_employee (should be 030532) is the same as employee_number. I think there seems to be a problem with me passing variables

employee_number seems to be printing fine, I get the value I want 030532 or should I say '30532'. I pass '030532' to the test_function as the first argument, but it doesn't seem to pass to the function when I print valid_employee is keep getting 12634?

IanS
  • 15,771
  • 9
  • 60
  • 84
  • Try to format and indent your code to make it more comprehensible. – hspandher Apr 28 '17 at 10:13
  • You have a `.` after `%`, remove it. – JkShaw Apr 28 '17 at 10:14
  • Please fix your indentation. – Daniel Roseman Apr 28 '17 at 10:14
  • sorry guys the full stop isn't supposed to be there, Daniel when you say fit my indentation are you referring to my code in this post? is it difficult to read. sorry guys I'm kinda new to stack overflow, first time using it. – Sid Micheals Apr 28 '17 at 10:20
  • check here, https://repl.it/H6EC I intented your code, you can test – akash karothiya Apr 28 '17 at 10:31
  • 1
    You try to pass the `valid_employee` value as a number. Why not trying strings? – Glrs Apr 28 '17 at 10:33
  • yes but in my if statement I am checking that the two values are the same. how will I be able to check a string and a decimal value? aren't they different ? – Sid Micheals Apr 28 '17 at 10:39
  • It has to do with the use of `int()`. The leading zero, in Python 2.7 means that the number is in octal system. In Python 3 leading zero is not allowed. I suggest you to use strings. – Glrs Apr 28 '17 at 10:39
  • @SidMicheals Both values should be strings. If you compare two strings then you will get the right result. – Glrs Apr 28 '17 at 10:41
  • yes even without the leading zero the value. thanks guys its just a little odd that I was getting '12346' I think maybe I had something wrong. anyway I am checking two strings against each other and it seems to be working thanks guys. its just a little annoying I can't use two integers. – Sid Micheals Apr 28 '17 at 10:42

2 Answers2

1

In Python 2.x number starting with 0 like 030532 is interpreted as octal and python inherently tries to convert it into decimal, hence in this case it gets converted to 13578.

>>> s = 032412
>>> s
    13578

In Python 3.xx

>>> t = 032412
  File "<stdin>", line 1
    t = 032412
             ^
SyntaxError: invalid token
JkShaw
  • 1,927
  • 2
  • 13
  • 14
1

Your problem is caused by an interaction between an odd design choice in Python 2 and an odd design choice in your program. The Python 2 design choice (fixed in Python 3) is that int literals with a leading 0 are interpreted as octal, not decimal. So we have:

>>> 030532
12634
>>> 010
8

This comes from C, where it apparently once made sense; the Python developers realised it is very surprising, and so this was changed in Python 3 - now a leading 0 is a syntax error.

The odd design in your program is that you are using integers here at all. Despite being called a 'number', an employee number is actually better treated as a string. If you use strings throughout your program for this, you will find it will magically work as you would expect. This is similar for phone numbers, credit card numbers, and other things you may come across. The 'rule' is:

A number is something it makes sense to do math on.

So, my age plus your age gives our total age, and halving that gives our average age. Age is a number. My employee number or phone number plus your employee number or phone number gives us... well, nonsense. If this kind of calculation is something that noone would ever want to do, use a string.

lvc
  • 34,233
  • 10
  • 73
  • 98