1

I have a problem with my Python script. When I run this Python script:

class Student:
    def __init__(self,student_name,student_id,student_phone):
        self.student_name = student_name
        self.student_id = student_id
        self.student_phone = student_phone

obj = Student("ELizaa",1253251,16165544)

print("student name",obj.student_name,"\nstudent id",obj.student_id,"\nStudent phone",obj.student_phone)

It is working fine. I got the expected output. But when student_phone starting with a 0 (like 0124575), I get an error:

obj = Student("ELizaa",1253251,016165544)
                                         ^
SyntaxError: invalid token

Why is this happening?

Timisorean
  • 1,388
  • 7
  • 20
  • 30
Eliza Meww
  • 19
  • 1
  • 3
  • I presume you want to use a string instead. – Kacperito Apr 06 '19 at 16:28
  • So i have to pass the value in string instead of Number ? – Eliza Meww Apr 06 '19 at 16:31
  • Yes. Based on the varialbe name 'student phone' I presume you want to store the number as base 10, but with leading zeros. The easiest is to store it as a string in that case. – Kacperito Apr 06 '19 at 16:33
  • 2
    Why do you not want it as a string? It's an identifier, it's not like you're going to do any numerical work on it, and you're presumably fixed by the bigger system in what values it can take. – roganjosh Apr 06 '19 at 16:41
  • @roganjosh Just I m curios . Is it possible or not . Based on everyone answer , seems it not possible :) – Eliza Meww Apr 06 '19 at 16:45
  • 1
    No, it isn't because, in the general case, you might come across a system that demands a 6 digit code; what will you do with `000001`? You can `zfill` but it will still be a string – roganjosh Apr 06 '19 at 16:46
  • Those student IDs & phone numbers are numeric strings, they aren't integers: you wouldn't do arithmetic on them (although you might want to sort them). So in your Python code you need to quote them. However, you can read numeric strings without quotes using `input()`, or from a file. – PM 2Ring Apr 06 '19 at 17:07

3 Answers3

1

In python3, you couldn't use 016165544 to create an integer variable. It's an octonary number in some other programming language, such as C. In Python, you should use 0o16165544 or 0O16165544.

However, what you want to create is a student ID and phone number, so I suggest that you use string instead.

Like this:

obj = Student("ELizaa", "1253251", "016165544")

cloudyyyyy
  • 461
  • 4
  • 9
1

In Python, adding 0 in front of any number needs an extra

  • x (for hexadecimal) followed by any number in hex digits range 0-9 or a-f or A-F.

  • o (for octal) followed by number(s) in octal digits range 0-7.

Have a look at below:

>>> 0o7
7
>>> 0o71
57
>>> 0o713
459
>>>
>>> 0xa
10
>>> 0xA
10
>>> 0x67
103
>>> 

» If you exceed the range or if you don't use x | o after 0.

>>> 0o8
  File "<stdin>", line 1
    0o8
     ^
SyntaxError: invalid token
>>> 
>>> 08
  File "<stdin>", line 1
    08
     ^
SyntaxError: invalid token
>>> 

Suggestion: If you are still willing to use 0 & want to perform operations on phones (for testing) then you can use the below approach to update the numbers.

Here we will store phone number as string & whenever we will update that, we will remove 0 from front, convert the remaining part into an integer, add (any operation) ant convert back to its original (0 in front) form & I think it is good.

>>> student_phone = "016165544"
>>> 
>>> # Add 3 to this
... 
>>> student_phone = "0" + str(int(student_phone.lstrip("0")) + 3)
>>> 
>>> student_phone
'016165547'
>>>  

Finally, you can call in this way (as you are already doing in your problem except 2nd one).

>>> class Student:
...     def __init__(self, student_name, student_id, student_phone):
...         self.student_name = student_name
...         self.student_id = student_id
...         self.student_phone = student_phone
... 
>>> obj = Student("ELizaa",1253251,16165544)
>>> print("student name",obj.student_name,"\nstudent id",obj.student_id,"\nStudent phone",obj.student_phone)
student name ELizaa 
student id 1253251 
Student phone 16165544
>>> 
>>> obj = Student("ELizaa",1253251,"016165544")                                
>>> print("student name",obj.student_name,"\nstudent id",obj.student_id,"\nStudent phone",obj.student_phone)
student name ELizaa 
student id 1253251 
Student phone 016165544
>>> 
hygull
  • 8,464
  • 2
  • 43
  • 52
0

Starting a number with the digit 0 makes it an octal number, but the behavior has some nuance. See this solution: Invalid Token when using Octal numbers

Paymahn Moghadasian
  • 9,301
  • 13
  • 56
  • 94