能组成几个单词

 

题目:(自己完成的)

输入一串字符串,计算看是不是包含f,r,i,e,n,d 并计算能组成 几个friend      

myStr = input("请输入一串字符串:")
num_f = num_r = num_i = num_e = num_n = num_d = 0
n=0;
#字典的值可以是变量
friend_dic ={'f':num_f, 'r':num_r, 'i': num_i, 'e':num_e, 'n':num_n, 'd':num_d}


for i in ['f', 'r', 'i','e', 'n','d']:
    for j in myStr:
        if i == j:
            friend_dic[i] += 1     #要用[],不要用()
n = friend_dic["f"]
for i in ['f', 'r', 'i','e', 'n','d']: 
    if n > friend_dic[i]:
        n = friend_dic[i]
print("一共有能组成%d个 %s" % (n, "friend"))

 

版本2.0

print("判断里面一字符串,里面的单词能组成几个'friend'")
print( "*" * 50)
myStr = input("请输入一串字符串: ")

count = len(myStr) // 5
for f in "friend":
    newCount = 0
    for s in myStr:
        if f == s:
            newCount += 1
    if count > newCount:
        count = newCount
print("能组成%d 个friend" % ( count))

 

版本3.0

posted @ 2017-08-18 22:14  bingxingc  阅读(4406)  评论(0编辑  收藏  举报