初学Python问题集锦

1、SyntaxError: Missing parentheses in call to 'print'

      遇到此类问题主要是版本问题,python 2可以提供print x,而python3只能print(x)

2、模块导入的两种方式
1)eg:
import math as myMath
print(myMath.sin(30))
2)eg:from 模块import 对象名【as别名】  #只能通过别名访问
from math import sin as mysin
print(mysin(30))

3、强制类型转换
TypeError: can't multiply sequence by non-int of type 'str'
eg:int(input())

4、正则表达式match匹配问题

match只从第一个字符开始匹配,只有第一个字符匹配上才匹配后面的,否则会提示下面错误:

  Traceback (most recent call last):
  File "C:/Users/Administrator/AppData/Local/Programs/Python/Python35/he.py", line 5, in <module>
    print(m.group(0))
AttributeError: 'NoneType' object has no attribute 'group'

若要选择match匹配,则需要加个判断或者异常处理即可,或者使用re.findall()或re.search()

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

import re
text="JGood is a handsome body,he is cool,clever,and so on"
m=re.match(r"(\w+)\s",text)
if m:
    print(m.group(0),'\n',m.group(1))
else:
    print("not match")
结果:================ RESTART: C:/Users/Administrator/Desktop/h.py ================
JGood  
  JGood

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

5、爬虫初尝(糗事百科)

-----------------------------------------------------------------------------------

内容太多就上个代码图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值