初学Python问题集锦

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_26781697/article/details/77542809

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、爬虫初尝(糗事百科)

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

内容太多就上个代码图

猜你喜欢

转载自blog.csdn.net/sinat_26781697/article/details/77542809