您的位置:首页 > 编程语言 > Python开发

初学Python问题集锦

2017-08-26 15:32 357 查看
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、爬虫初尝(糗事百科)

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

内容太多就上个代码图
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python