浙大版《Python程序设计》题目集(函数篇)
def fn(a,n):
sum = 0
for i in range(n):
sum = sum + a * pow(10,i) * (n - i)
return sum
a,n = input().split(" ")
s = fn(int(a),int(n))
print(s)
def prime(p):
global flag
if p != 1:
if p == 2:
flag = True
else:
for i in range(2,p):
if p % i == 0:
flag = False
break
else:
flag = True
continue
else:
flag = False
return flag
def primeSum(m,n):
sum = 0
for i in range(m,n + 1):
flag = prime(i)
if flag:
sum = sum + i
continue
else:
continue
return sum
m,n = map(int,input().split(" "))
print(primeSum(m, n))
def CountDigit(number,digit):
number = str(number)
count = 0
for num in number:
if num.isdigit():
if int(num) == digit:
count = count + 1
else:
continue
return count
number,digit = input().split(" ")
number = int(number)
digit = int(digit)
count = CountDigit(number, digit)
print("Number of digit",digit,"in",str(number)+":",count)
第6章函数-4 使用函数输出指定范围内Fibonacci数的个数
def fib(n):
count = 2
f0 = f1 = 1
res = 1
while count <= n:
res = f0 + f1
f0 = f1
f1 = res
count = count + 1
return res
def PrintFN(m,n):
ls = []
fibs = []
for j in range(1,n + 1):
fibs.append(fib(j))
for i in range(m,n + 1):
if i in fibs:
ls.append(i)
return ls
m,n,i = input().split()
n = int(n)
m = int(m)
i = int(i)
b = fib(i)
print("fib({0}) = {1}".format(i,b))
fiblist = PrintFN(m,n)
print(len(fiblist))
#自定义阶乘求解函数
def factorial(n):
res = 1
for i in range(1,n + 1):
res = res * i
return res
#自定义余弦函数近似值求解函数
def funcos(eps,x):
value,sgn,index= 0,1,0
error = sgn * (x ** index) / factorial(index)
while abs(error) > eps or abs(error) == eps:
value = value + error
sgn = - sgn
index = index + 2
error = sgn * (x ** index) / factorial(index)
return value
eps = float(input())
x = float(input())
value = funcos(eps,x)
print("cos({0}) = {1:.4f}".format(x,value))
def acronym(phrase):
ls = phrase.split(" ")
abbr = ''
for word in ls:
if len(word) > 0:
abbr = abbr + word[0].upper()
else:
continue
return abbr
phrase = input()
print(acronym(phrase))
本文来自博客园,作者:闲晚,转载请注明原文链接:https://www.cnblogs.com/centimeter73/p/15472978.html