Python学习——常见运行错误

转载:https://www.oschina.net/question/89964_62779


1、错误提示:AttributeError:'str' object has no attribute 'append'

     错误示例:for x in name[1:]:

                    a.append(x.lower())

     错误说明:append方法是列表方法,而lower()函数的定义是返回字符串中所有大写字符转换为小写后生成的字符串,即                        返回的是字符串,字符串没有append方法,字符串和列表添加元素的方法如下:

                     >>>str = 'pyt'                                            >>>lis=[p,y,t,h,o]

                     >>>str = str + 'hon'                                   >>>lis.append(n)

                     >>>str                                                       >>>lis

                     'python'                                                      [p,y,t,h,o,n]


2、错误提示:SyntaxError:invalid syntax       

     造成这个错误的原因就太多了,大致有下列原因造成:

     ①缩进的使用,该缩进的地方没缩进,不该缩进的地方使用了缩进

     ②漏掉':',对于新手来说,这是特别容易犯的错误

     ③赋值符号‘=’和比较操作'=='的混用




          

猜你喜欢

转载自blog.csdn.net/weixin_42156385/article/details/80346539