lxd670
发表于 2019-12-15 21:34:06
谢谢分享
kinkon
发表于 2019-12-16 10:37:06
def fun(n):
sum = 0
if n%2 == 0:
for x in range(2, n+1, 2):
sum += 1/x
else:
for x in range(1, n+1, 2):
sum += 1/x
print(sum)
初学者,只会简单的
天下一统2014
发表于 2019-12-18 21:50:32
def c(n,a):
b = []
while True:
b.append(n)
if n < a:
n += 2
else:
break
return b
a = input("请输入多少人:")
while a.isdigit() is False:
a = input("请重新输入多少人:")
a = int(a)
if a%2 == 0:
b = c(2,a)
else:
b = c(1,a)
d = 1
for i in range(len(b)):
if i < (len(b)-1):
d /= (b+1)
else:
d /= b
print(d)
天下一统2014
发表于 2019-12-18 21:54:41
def c(n,a):
b = []
while True:
b.append(n)
if n < a:
n += 2
else:
break
return b
a = input("请输入多少人:")
while a.isdigit() is False:
a = input("请重新输入多少人:")
a = int(a)
if a%2 == 0:
b = c(2,a)
else:
b = c(1,a)
d = 0
for i in range(len(b)):
d += 1/b
print(d)
ouyunfu
发表于 2020-2-13 00:39:07
import fractions
def f15(n):
if n%2==0:
res=0
while n>=2:
res+=1/n
n-=2
else:
res=0
while n>=1:
res+=1/n
n-=2
return res
print(f15(49))
落日余光
发表于 2020-2-21 09:09:37
def funcion1(n):
def function2(i):
result=0
while i>=1:
result+=(1/i)
i-=2
return result
if n%2==0:
b=function2(n)
else:
b=function2(n)
return b
temp=input('请输入一个大于0的整数:')
n=int(temp)
a=funcion1(n)
print('结果是:',a)
xuanyuan0116
发表于 2020-2-24 11:11:17
print('----------每日练习题15----------')
#题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n
def fun(x):
list1 = []
list2 = []
if x % 2 == 0:
list1 = list(range(1, x+1))
list2 = list1
count = 0
for i in range(len(list2)):
count += (1/list2)
print('当输入n为偶数时,数据结果为%f' %count)
elif x % 2 != 0:
list1 = list(range(1, x+1))
list2 = list1[::2]
count = 0
for i in range(len(list2)):
count += (1/list2)
print('当输入n为奇数时,数据结果为%f' %count)
ruokang
发表于 2020-2-24 12:22:37
def fun15(n):
sum = 0
while n > 0:
sum = sum + 1/n
n -= 2
return sum
print(fun15(3))
嘿嘿嘿的黑子哥
发表于 2020-2-27 11:47:19
def fun(number):
list1 =[]
sum1 = 0
if number % 2 ==0:
for i in range(2,number+1,2):
a = float(1/i)
sum1 += a
list1.append(str(1/i))
print("各分数的加法求和的表达式为:","+".join(list1))
print("其和为:",sum1)
print("汇总为:","+".join(list1),"=",sum1)
else:
for i in range(1,number+1,2):
a = float(1/i)
sum1 += a
list1.append(str(1/i))
print("各分数的加法求和的表达式为:","+".join(list1))
print("其和为:",sum1)
print("汇总为:","+".join(list1),"=",sum1)
海上飞鱼
发表于 2020-3-1 09:58:42
from functools import reduce
def fracsum3(n):
return reduce(lambda x,y:x+1/y, range(n,0,-2), 0)
catwine1990
发表于 2020-3-8 19:22:43
#分段函数调用
def f(x):
f1(x) if x%2!=0 else f2(x)
def f1(x):
sum=0
for i in range(1,x+1,2):
sum+=1/i
print(sum)
def f2(x):
sum=0
for i in range(2,x+1,2):
sum+=1/i
print(sum)
再见二丁目27
发表于 2020-3-8 20:49:28
n = int(input('请输入一个整数:'))
def s(n):
c = 0
if n % 2 == 0:
for i in range(2,n+1,2):
c += 1/i
else:
for i in range(1,n+1,2):
c += 1/i
print(c)
s(n)
杜若左
发表于 2020-3-11 07:37:43
谢谢楼主
杜若左
发表于 2020-3-11 07:40:23
j = int(input("请输入一个数:"))
k =0
if j % 2==0:
for i in range(2,j+1,2):
k = k+ 1/i
else :
for i in range(1,j+1,2):
k = k+ 1/i
print("结果为",k)
嘿嘿
HimLee
发表于 2020-3-20 15:38:24
学习一下
holiday_python
发表于 2020-3-27 17:59:12
def func(n):
sum_num = 0
if n % 2 == 0:
print('该数为偶数, 结果为: ')
list_even =
print(list_even)
for m in list_even:
sum_num += m
else:
print('该数为奇数, 结果为: ')
list_odd =
print(list_odd)
for j in list_odd:
sum_num += j
return sum_num
num = int(input('请输入n值: '))
print(func(num))
听风夜雨
发表于 2020-3-29 22:48:13
def f(n):
x=0
if n%2==0:
for i in range(2,n+1,2):
x+=1/i
else:
for i in range(1,n+1,2):
x+=1/i
return x
#example
f(6)
一只235
发表于 2020-3-30 10:12:09
def ou(n):
if n==2:
return 1/2
elif n>3 :
return 1/n+ou(n-2)
def ji(n):
if n==1:
return 1
elif n>1:
return 1/n+ji(n-2)
x=int(input("qingshur"))
if x<1:
print("error")
else :
if x%2==0 :
print("{}".format(ou(x)))
elif x%2!=0:
print("{}".format(ji(x)))
君子好逑
发表于 2020-4-12 13:13:37
n=input('请输入一个整数:')
n=int(n)
result=0
if (n%2==0):
for each in range(1,n+1):
if (each%2==0):
result=result+1/each
else:
for each in range(1,n+1):
if (each%2==1):
result=result+1/each
print(result)
danianyu
发表于 2020-4-12 21:42:03
除法运算是哪个函数
页:
1
2
3
4
5
6
7
8
9
[10]
11
12