第4章-12 求满足条件的斐波那契数
a=eval(input()) b=c=1 d=1 for i in range(a): c=b b=d d=b+c if d>a: print(‘{}‘.format(d)) break
第4章-13 求误差小于输入值的e的近似值
a=eval(input()) b=1 count=1 count2=0 for i in range(1,100000): b=b*i count2=count count=count+1/b if (count-count2)<a: print(‘{:6f}‘.format(count)) break
第4章-14 统计字符
参考了别人的代码:
s=[] count=0;letters=0;space=0;digit=0;others=0 while True: b=list(input()) count=count+1 s.extend(b) if len(s)+count>10: count=count-1 break for c in s: if c.isalpha(): letters+=1 elif c.isspace(): space+=1 elif c.isdigit(): digit+=1 else: others+=1 print(‘letter = {}, blank = {}, digit = {}, other = {}‘.format(letters,space+count,digit,others))
用列表输入,用count算回车数,如果合计大于10就退出循环,下面调用python自带函数计算
原文地址:https://www.cnblogs.com/echoDetected/p/12340811.html
时间: 2024-10-24 17:17:05