当前位置: 移动技术网 > IT编程>脚本编程>Python > Python学习常见十大问题总结

Python学习常见十大问题总结

2020年03月26日  | 移动技术网IT编程  | 我要评论

鬼四忌,徐沛东简历,陈公弼传


         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

        答案:字符串不能跟整型合并,把需要合并的整型用str转换成字符串即可。


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


         "c:\program files\python\python36\python.exe" h:/python培训/2017-09-18/level3.py

          file "h:/python培训/2017-09-18/level3.py", line 2

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

                    ^

         syntaxerror: invalid syntax

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


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

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


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

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


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

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

          "c:\program files\python\python36\python.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语句



7: 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将字符串转换成整型即可。


8: 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将整型转换为字符串即可。


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


           file "h:/python培训/1.第一模块/作业/模拟登陆/login.py", line 6

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

                                         ^

          syntaxerror: invalid syntax


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

10: 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就业还是兴趣爱好,记住:项目开发经验永远是核心,如果你缺新项目练习或者没有python精讲教程,可以去小编的python交流.裙 :七衣衣九七七巴而五(数字的谐音)转换下可以找到了,里面很多新教程项目,还可以跟老司机交流讨教!

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网