我的Python脚本有问题。当我运行这个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)
它工作得很好。我得到了预期的产量。但是当student_phone
以0
开头时(就像0124575
),我得到了一个错误:
为什么会这样?在
在python3中,不能使用}。在
016165544
来创建一个整数变量。在其他编程语言中,它是一个八进制数,例如C
。在Python中,应该使用0o16165544
或{但是,您想要创建的是一个学生ID和电话号码,所以我建议您使用
string
。在像这样:
在Python中,在任何数字前面添加
0
需要额外的x
(用于十六进制)后跟十六进制数字范围内的任何数字0-9
或{o
(对于八进制)后跟八进制数字范围内的数字0-7
。请看以下内容:
^{pr2}$最后,你可以这样打电话(除了第二个问题外,你已经在处理你的问题了)。在
以数字0开头的数字使其成为八进制数,但行为有一些细微差别。请参阅此解决方案:Invalid Token when using Octal numbers
相关问题 更多 >
编程相关推荐