python最小值判断_python ,统计数字,最长最短的单词。以及,怎么判断最大最小值...

匿名用户

1级

2011-09-30 回答

def num_input():

count=1

data_list=[]

while True:

count += 1

data=input("enter a number:")

if data == "":

print(count-2,"numbers entered")

break

number=float(data)

data_list.append(number)

print("sum:",sum(data_list),"max:", max(data_list), "min:", min(data_list))

def count_in_str(s):

words=[w for w in s.split() if w.isalpha()]

words_len=[len(w) for w in words]

print("Longest word(s):",",".join(filter(lambda s:len(s)==max(words_len),words)))

print("Shortest word(s):", ",".join(filter(lambda s:len(s)==min(words_len),words)))

print(len([c for c in s if c.isnumeric()]), "numbers in the string")

print(len([c for c in s if c in [' ', '\t']]), "spacess in the string")

print(len([c for c in s if c.isalpha()]), "characters in the string")

追问:

不好意思,还要你麻烦解释一下,不运行怎么办,我的版本3.2.2

追答:

我的也是3.2,我调试好了才贴出来的。你把错误贴一下吧,百度的代码缩进有问题。我帮你贴到ideone去。

看这里

http://ideone.com/entaD

追问:

我不明白怎么回事。写好后,用python shell测试时,什么都没有,出了两行>>>,就结束了,也没有错误啊。

另外,能详解怎么用def ,或者有什么好的讲解介绍给我,

非常非常非常感谢你的帮助

追答:

出了两行>>的话是因为只定义了函数但是没有调用它。

加上这两行,定义的函数就会执行了。

num_input()

count_in_str("a string with soooooooo many words and numb3rs !")

至于def的使用,这是python中相当基础的问题,我建议你直接去看书。

http://ishare.iask.sina.com.cn/f/19282344.html

这里有本不错的入门。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值