0012-编程打卡:按如下要求编写分数计算函数
本帖最后由 不二如是 于 2022-8-30 17:44 编辑一星答案:
#偶数求和
def peven(n):
i = 0
s = 0.0
for i in range(2,n + 1,2):
s += 1.0 / i
return s
#奇数求和
def podd(n):
s = 0.0
for i in range(1, n + 1,2):
s += 1 / i
return s
if __name__ == '__main__':
n = int(input('请输入整数n:\n'))
if n % 2 == 0:
sum = peven(n)
else:
sum = podd(n)
print(sum)
二星答案:
def fishc(n):
s = 0
if n%2 ==0:
for i in range(1,n+1):
if i%2 ==0:
num = 1/i
s =s + num
print(s)
else:
for i in range(1,n+1):
if i%2 !=0:
num = 1/i
s =s + num
print(s)
if __name__ == '__main__':
n = int(input('请输入整数n:\n'))
fishc(n)
三星答案:
**** Hidden Message *****
基础语法:
https://www.bilibili.com/video/BV1c4411e77t
算法讲解:
https://www.bilibili.com/video/BV1HT4y1K7DY
越努力越幸运 越努力越幸运
Python 猜对了吗
越努力越幸运
Python 好好玩 越努力越幸运 越努力越幸运 {:5_106:}越努力越幸运 越努力越幸运
页:
[1]