简体   繁体   English

语法错误:无效的令牌 python 3

[英]SyntaxError: invalid token python 3

I have a problem with my Python script.我的 Python 脚本有问题。 When I run this Python script :当我运行这个Python 脚本时

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 :但是student_phone0开头(如0124575 )时,我收到一个错误

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

Why is this happening?为什么会这样?

In python3, you couldn't use 016165544 to create an integer variable.在 python3 中,您不能使用016165544来创建整数变量。 It's an octonary number in some other programming language, such as C .它是其他一些编程语言(例如C的八进制数。 In Python, you should use 0o16165544 or 0O16165544 .在 Python 中,您应该使用0o161655440O16165544

However, what you want to create is a student ID and phone number, so I suggest that you use string instead.但是,您要创建的是学生证和电话号码,因此建议您使用string

Like this:像这样:

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

In Python, adding 0 in front of any number needs an extra在 Python 中,在任意数字前加0需要额外的

  • x (for hexadecimal) followed by any number in hex digits range 0-9 or af or AF . x (对于十六进制)后跟十六进制数字范围0-9afAF的任何数字。

  • o (for octal) followed by number(s) in octal digits range 0-7 . o (对于八进制)后跟八进制数字范围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 | » 如果超出范围或不使用x | o after 0 . o0之后。

>>> 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.建议:如果您仍然愿意使用0并希望在手机上执行操作(用于测试),那么您可以使用以下方法更新数字。

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.在这里,我们将电话号码存储为字符串 & 每当我们更新它时,我们将从前面删除0 ,将剩余部分转换为整数,添加(任何操作)ant 转换回其原始(前面的0 )形式 & 我认为这很好。

>>> 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
>>> 

Starting a number with the digit 0 makes it an octal number, but the behavior has some nuance.以数字 0 开头的数字使其成为八进制数,但行为有一些细微差别。 See this solution: Invalid Token when using Octal numbers请参阅此解决方案: Invalid Token when using Octal numbers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM