片段代码练习之【水仙花,三角形,字符统计】

#统计字符个数方法
def count_char(char,string):
    count=0
    for c in string:
        if c==char:
            count+=1

    return  count

char='l'
string='hello world'
count =count_char(char,string)
print('{0}字符个数为:{1}'.format(char,count))

#判断三角形
def is_triangle(a,b,c):
    if a+b>c and abs(a-b)<c:
        return True
    else:
        return False

print(is_triangle(2,3,1))

#水仙花判断
def is_narcissistic_num(num):
    count=0
    for i in str(num):
        count=pow(int(i),3)+count

    if count==num:
        return True
    else:
        return False

print(is_narcissistic_num(153))

  

posted @ 2024-02-23 12:32  小戳同学  阅读(8)  评论(0编辑  收藏  举报