匿名用户
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
这里有本不错的入门。