pythonifend
‘壹’ python问题:鹦鹉学舌,定义类 Bage(八哥),使之能随机复述听过的话
import random
class Bage():
def __init__(self):
self.sentences = []
def hear(self, msg):
self.sentences.append(msg)
def repeat(self):
start = 0
end = len(self.sentences)
if end:
i = random.randint(start, end - 1)
print self.sentences[i]
else:
print "none msg"
a_bage = Bage()
a_bage.hear("hello")
a_bage.hear("hello1")
a_bage.hear("hello2")
a_bage.hear("hello3")
a_bage.repeat()
~
‘贰’ python编程:下列代码表示什么含义
1 这是一个 Fibonacci 数列的计算函数,使用了递归的方法
f(n) = 1, n=1
f(n) = n*f(n-1), n>1
2 这个只有函数,没有执行代码
可以加上 print fib(2) 之类的
3 记住这是 Python,靠缩进来区分代码的分级,没有 end if 的语法
=========================================
函数开始处不是写了输出语句吗: print 'n =', n
return 是返回值啊
值可以用来给其他的变量赋值,或用于其他的函数中
如 x=fib(3)
没有返回值的话,就只能单独调用了
fib(3)
‘叁’ 关于python的if语句的格式问题
#python3的代码
defcount_letters(s,char,n):
end=len(s)
count=0
ifn<0:n=0
whilen<=end:
r=str.find(s,char,n)
ifr!=-1:
n=r+1
count+=1
else:break
returncount
t="hollowooorld"
print(count_letters(t,'o',0))
#函数版
defcount_letters2(text,ch,start):
ifstart<0:start=0#x
returnlen(list(filter(lambdac:c==ch,list(text)[start:])))
print(count_letters2(t,'o',0))
5
5
‘肆’ 关于python的重载问题
没有重载,但是可以有默认参数和不定长参数,可以判断默认值和参数长度来处理。
比如:
def range(start, end = -1):
if end == -1:
end = start
start = 0
或
def range(*args):
if len(args) == 1:
start = 0
end = args[0]
elif len(args) == 2:
start, end = args[0], args[1]
‘伍’ 如何在Python中使用break跳出多层循环
python中的break默认只能退出当前循环,无法退出多重循环。不过想退出多重循环可以用退出标志的方式来折中实现。代码如下。
endloop1=False
whileTrue:
endloop2=False
ifendloop1:
print('endloop1')
break
whileTrue:
endloop3=False
ifendloop2:
print('endloop2')
break
i=0
whileTrue:
ifendloop3:
print('endloop3')
break
ifi>3:
endloop1=True
endloop2=True
endloop3=True
i+=1
不明白可追问
‘陆’ python3 的if语句 问题
04142434445464748495051525354# 第一首先要有缩进 因为python中是以缩进区分不同的代码块# eg1:if ...: ....if ...: ...# eg2:if ...: ..... if ...: ....# eg1 和 eg2 是两种代码块# eg1会走完第一个if后 条件成立与否 都会继续走第二个if # eg2则当第一个if条件不成立时 不会走第二个if# 这是python最基础要掌握的 通过题主的问题 不知道题主掌握没…所以多说一嘴(因为我看到下面有追问 可能是这个原因) #另外 如果想要python帮你完成条件1条件2条件3某一成立则执行某代码块的话# eg3:if ...: ... #当满足条件时 会执行这裏的代码块 然后调到最后elif ...: ... #当满足条件时 会执行这裏的代码块 然后调到最后elif ...: ... #当满足条件时 会执行这裏的代码块 然后调到最后else: ... #当以上条件都不满足时 执行这裏的代码块 当然可以没有该代码块 取决与coder #所谓“当条件符合第一个第二个if语句,为什么输出结果的同时还是输出else的结果而条件符合第三个则不会输出else”#我猜想代码块是这样的: if ...: ... if ...: ...if ...: ... if ...: ...else: ... # 当然 还要看你的条件是什么 不太好判断#还有可能 使用了breakif ...: break print("test") # 你会发现 test并没有显示出来…#最后 期待题主贴出源代码!
‘柒’ python里的end是什么意思
python里的end是print函数中的参数,为末尾end传递一个字符串,这样print函数不会在字符串末尾添加一个换行符,而是添加一个字符串,其实这也是一个语法要求,表示这个语句没结束。end意思就是以给定字符串或制表符结尾,但是不默认换行。(推荐学习:Python视频教程)
在python3中,默认print()为换行,比如图:
此处是一个乘法口诀代码,但是在python3中,默认print后为换行的,此处为了不换行,就需求改变输出方式了,其实这也是一种语法结构,如图:
此处的end="t"是制表符的意思,及打印一个制表符,不换行,
另外,end="n"也是换行的意思
更多Python相关技术文章,请访问Python教程栏目进行学习!以上就是小编分享的关于python里的end是什么意思的详细内容希望对大家有所帮助,更多有关python教程请关注环球青藤其它相关文章!
‘捌’ python if 语句如何书写
第三行前面应该也有三个点,怎么没有了,第二行结束后按的是回车么。还有对于python的子句和嵌套关系都是又空格来确定的,在命令行运行尽量用tab键。
如果某个子句没有内容,那么也不能是空的,也就是冒号:包含的块即使没有东西,也得写一个pass,如果想结束子块,在命令行下,要按两行enter。
或者
if <条件> then <语句> ;
注意:Pascal中也有if 的嵌套,但else只承接最后一个没有承接的if,如:
if <条件1> then if <条件2> then <语句1> else <语句2>; 此处<语句2>当且仅当<条件1>成立且<条件2>不成立时运行。
if <条件1> then begin if <条件2> then <语句1> end else <语句2>; 此处<语句2>只要<条件1>成立就运行。
‘玖’ python提示语法错误,,求帮助
defgetImg(html):
reg=r'<url>[^<]*[^u]*[^r]*[^l]*[^>]*</url>'
imgae=re.compile(reg)#comile拼写错了
imglist=re.findall(imgae,str(html))
returnimglist
#正则也需要修改下,如下:
importre,urllib
defsfinds(start_str,end,html):
start=html.find(start_str)
ifstart>=0:
start+=len(start_str)
end=html.find(end,start)
ifend>=0:
returnhtml[start:end].strip()
defgetHtml(url):
p=urllib.urlopen(url)
html=p.read()
returnhtml
defgetImg(html):
reg=r'<imgs*srcs*="?(S+)"?'
imgae=re.compile(reg)
imglist=re.findall(imgae,str(html))
returnimglist
printgetImg(getHtml('http://image..com/'))
如果解决了您的问题请采纳!
如果未解决请继续追问
‘拾’ python如何判断if语句结束的位置
根据缩进吧,每个if后面都必须强制缩进,写到后面没有缩进了的话这个if就结束了(如果没有加elif或者else的话)。没有括号之类的明显标识,不过在sublimetext里面倒是可以看到虚线。