第一关 温度转换
# 请在此添加代码
########## Begin ##########
def convert(c):
return c*9/5+32
########## End ##########
C = eval(input( "input a number:" ))
F = convert( C )
print( "%.1f"%F )
第二关求五边形面积
# 请在此添加代码
from math import *
########## Begin ##########
x = input().split(',')
#x = x.split(',')
x = list(map(int,x))#将列表里的字符串变为数字,方便后面直接调用
# 还有一种是列表解析 x = [int(i) for i in x]
def func(a,b,c):
p = (a+b+c)/2
s = pow((p*(p-a)*(p-b)*(p-c)),0.5) #**0.5
return s
def main():
s1 = func(x[0],x[1],x[5])
s2 = func(x[5],x[2],x[6])
s3 = func(x[6],x[4],x[3])
return s1+s2+s3
print('area={:.5f}'.format(main()))
第三关匿名函数应用
# 请在此添加代码
from math import pi,log,exp,e
n=int(input("Please Input n:y="))
def func(r):
an = (1+log(r,e))/(2*pi) #lnr表示为log(r,e)
return an
y=e**2 #exp(2) 给y赋值为e^2
for n in range(1,n+1):
y += func(n)
#遍历求和
print('{:.5f}'.format(y))
第四关函数调用求累加和
# 请在此添加代码
########## Begin ##########
def mysum(n,m):
y = 0
for i in range(1,int(n+1)):
an = i**m
y = y+an
return y
n = int(input('Please Input n:'))
if n%10 == 0:
s = mysum(n,1) + mysum(n/2,2) + mysum(n/10,-1)
print('s={:.5f}'.format(s))
else:
print('input error')
########## End ##########
第五关递归法求和
# 请在此添加代码
########## Begin ##########
def p(x,n):
while n>0: #函数定义
s = 0
if n==1:
s = x
else:
s = x*(1-p(x,n-1))
return s
########## End ##########
x,n=eval(input("请依次输入x,n的值:"))
s=p(x,n)
print("p(%f,%d)=%.2f"%(x,n,s))
第六关求满足条件的分数
# 请补充完善代码
########## Begin ##########
from math import *
def isprime(n):
if n > 1:
for i in range(2,n):
if n%i == 0:
return False
break
else:
return True
else:
return False
def main():
count = 0
a=int(input("please input a:"))
b=int(input("please input b:"))
if(a<0 or b<0 or a<=b):
print("Input Error")
else:
for n in range(10,99):
if isprime(n)==True:
for m in range(2,100):
if isprime(m)==True:
x = m/n
if x>=(1/a) and x<=(1/b):
count+=1
##以下为求出满足条件的分数的个数
print("满足条件的数有{:d}个".format(count))
main() ##调用main函数实现程序的功能
########## End ##########