1.编写一个函数,求1+2+3+...+N
def sum1(n):
sum1 = 0
for i in range(1,n+1):
sum1 += i
print(sum1)
sum1(7)
2、编写一个函数,求多个数中的最大值
lst = []
str = raw_input("请输入数值,用空格隔开:")
lst1 = str.split(" ")
i = 0
while i <= len(lst1)+1:
lst.append(int(lst1.pop()))
i += 1
def sum(list):
s = 0
for x in list:
s += x
return s
3、编写一个函数,实现摇色子的功能,打印N个色子的点数和
import random
def sum1(n):
sum3 = 0
for i in range(n):
sum2 = random.randint(1, 6)
sum3 +=sum2
print("骰子和:",sum3 )
sum1(5)
5、编写一个函数,求三个数中的最大值
def max1 (a,b,c):
if a>b and a>c:
print("最大值是:",a)
if b>a and b>c:
print("最大值是:", b)
if c>a and c>b:
print("最大值是:", c)
max1(1,2,3
7.编写一个函数,求多个数的平均值
lst = []
str = raw_input("请输入数值,用空格隔开:")
lst1 = str.split(" ")
i = 0
while i <= len(lst1)+1:
lst.append(int(lst1.pop()))
i += 1
def sum(list):
s = 0
for x in list:
s += x
return s
def average(list):
avg = 0
avg = sum(list)/(len(list)*1.0)
return avg
8、编写一个函数,默认求10的阶层,也可以求其他数的阶层
def my_fun(n):
jie =1
for i in range(1,n+1):
jie1 =jie*i
jie =jie1
print(jie1)
my_fun(10)
网友评论