python的常见问题_Python学习常见十大问题总结

Traceback (most recent call last):File "H:/Python培训/2017-09-14/Shopping Cart.py", line 6, in 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.pyFile "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 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 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 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 40elif 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 if choice >=0 and choice=' 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 ba.write(bala)TypeError: write() argument must be str, not int答案:write写入必须为字符串,不能为整型,使用str将整型转换为字符串即可。9: win 系统下,python3+环境,使用split函数时,执行后报错信息如下File "H:/Python培训/1.第一模块/作业/模拟登陆/login.py", line 6x = line1.strip().split(,)^SyntaxError: invalid syntax答案:split中的参数为字符串类型,必须加引号。10: win 系统下,python3+环境,写一个程序进行文件操作,读取文件时,执行后报错信息如下Traceback (most recent call last):File "H:/Python培训/1.第一模块/作业/三级菜单/level3.py", line 2, in for line in f:io.UnsupportedOperation: not readable答案:打开文件时使用的是“a”追加模式,无法读取文件,将打开模式改为“r”即可。问题: win 系统下,python3+环境,for循环一个字典时,执行后报错信息如下File "H:/Python培训/1.第一模块/作业/三级菜单/level3.py", line 18for y in level[choice]^SyntaxError: invalid syntax答案:for循环语句末尾忘记加:,加上:即可。问题: win 系统下,python3+环境,写一个程序对文件进行操作,返回文件大小时,执行后报错信息如下Traceback (most recent call last):File "H:/Python培训/1.第一模块/作业/购物车/shopping.py", line 22, in if os.path.getsize() == 0:TypeError: getsize() missing 1 required positional argument: 'filename'答案:os.path.getsize()的参数必须为具体的文件,指定具体的文件即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值