2020最新Python学习常见十大问题总结


         Traceback (most recent call last):

         File "H:/Python培训/2017-09-14/Shopping Cart.py", line 6, in <module>

         print("您选择的商品价格为:"+prices[buy+1])

         TypeError: must be str, not int




问题: win 系统下,python3+环境,在创建一个嵌套的字典时,执行后报错信息如下


         "C:Program FilesPythonPython36python.exe" H:/Python培训/2017-09-18/level3.py

          File "H:/Python培训/2017-09-18/level3.py", line 2

         "福建省"{"福州"{"仓山区","晋安区","台江区"},

                    ^

         SyntaxError: invalid syntax

        答案:键跟值之间必须要使用:隔开,加上:即可。


问题: win 系统下,python3+环境,在读取一个嵌套的字典时,执行后报错信息如下

   Traceback (most recent call last):

   File "H:/Python培训/2017-09-18/level3.py", line 14, in <module>

   for i2 in level(choice1):

   TypeError: 'dict' object is not callable

          答案:字典取键的值时应该使用[]而不是(),()改成[]即可。


问题: win 系统下,python3+环境,在引用一个变量时,执行后报错信息如下


   Traceback (most recent call last):

   File "H:/Python培训/2017-09-19/shopping car.py", line 22, in <module>

   print(seller_username)

   NameError: name 'seller_username' is not defined

         答案:引用了一个不存在的变量,引用前需要先定义变量。


问题: win 系统下,python3+环境,在调用一个列表的值时,执行后报错信息如下


   Traceback (most recent call last):

   File "H:/Python培训/2017-09-19/shopping car.py", line 26, in <module>

   if user_password == user_password_list(user_list.index(user_name)):

   TypeError: 'list' object is not callable

          答案:取列表的值时应该使用[]而不是(),把()换成[]即可。
问题: win 系统下,python3+环境,在执行一个if..else语句时,执行后报错信息如下

          "C:Program FilesPythonPython36python.exe" "H:/Python培训/2017-09-19/shopping car.py"

           File "H:/Python培训/2017-09-19/shopping car.py", line 40

           elif choice == "q":

                                   ^

          IndentationError: expected an indented block

        答案:if语句下面的代码块没写就运行,代码块没写时可使用pass语句



问题: win 系统下,python3+环境,choice接收input的输入,执行后报错信息如下


        Traceback (most recent call last):

        File "H:/Python培训/2017-09-19/shopping car.py", line 40, in <module>

        if choice >=0 and choice<len(goods_list):

       TypeError: '>=' not supported between instances of 'str' and 'int'

      答案:数据类型错误,input输入的为字符串,字符串无法跟整型比较,使用int将字符串转换成整型即可。


问题: win 系统下,python3+环境,进行文件操作时,执行后报错信息如下

           Traceback (most recent call last):

           File "H:/Python培训/2017-09-19/shopping car.py", line 56, in <module>

           ba.write(bala)

           TypeError: write() argument must be str, not int

          答案:write写入必须为字符串,不能为整型,使用str将整型转换为字符串即可。


问题: win 系统下,python3+环境,使用split函数时,执行后报错信息如下


           File "H:/Python培训/1.第一模块/作业/模拟登陆/login.py", line 6

           x = line1.strip().split(,)

                                         ^

          SyntaxError: invalid syntax


         答案:split中的参数为字符串类型,必须加引号。

问题: win 系统下,python3+环境,写一个程序进行文件操作,读取文件时,执行后报错信息如下


          Traceback (most recent call last):

          File "H:/Python培训/1.第一模块/作业/三级菜单/level3.py", line 2, in <module>

          for line in f:

          io.UnsupportedOperation: not readable


         答案:打开文件时使用的是“a”追加模式,无法读取文件,将打开模式改为“r”即可。
问题: win 系统下,python3+环境,for循环一个字典时,执行后报错信息如下


            File "H:/Python培训/1.第一模块/作业/三级菜单/level3.py", line 18

            for y in level[choice]

                                          ^

            SyntaxError: invalid syntax


          答案:for循环语句末尾忘记加:,加上:即可。


问题: win 系统下,python3+环境,写一个程序对文件进行操作,返回文件大小时,执行后报错信息如下


           Traceback (most recent call last):

           File "H:/Python培训/1.第一模块/作业/购物车/shopping.py", line 22, in <module>

           if os.path.getsize() == 0:

           TypeError: getsize() missing 1 required positional argument: 'filename'

          答案:os.path.getsize()的参数必须为具体的文件,指定具体的文件即可。
最后
注意:不管你是为了Python就业还是兴趣爱好,记住:项目开发经验永远是核心,如果你没有2020最新python入门到高级实战视频教程,可以去小编的Python交流.裙 :七衣衣九七七巴而五(数字的谐音)转换下可以找到了,里面很多新python教程项目,还可以跟老司机交流讨教!

本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

原文地址:https://www.cnblogs.com/chengxuyuanaa/p/12836992.html