- 编写一个函数,求1+2+3+...+N
def my_sum2(n):
print(sum(range(n+1)))
my_sum2(4)
课条:
def yt_sum(n):
sum1 = 0
for x in range(1, n+1):
sum1 += x
return sum1
print('第1题:', yt_sum(100))
- 编写一个函数,求多个数中的最大值
def my_max(n):
print(max(range(n+1)))
my_max(3)
课条:
def yt_max(*nums):
# 最大值默认是第一个数
max1 = nums[0]
for item in nums:
if item > max1:
max1 = item
return max1
print('第2题:', yt_max(12, 34, 6, 87, 54))
- 编写一个函数,实现摇骰子的功能,打印n个骰子的点数和
def my_sum1(n):
import random
sum2 = 0
x = 1
while x < n:
num = random.randint(1, 6) # 产生随机点数
sum2 += num
n += 1
return sum2
my_sum1(4)
课条:
from random import randint
def play_dice(n):
sum1 = 0
for _ in range(n):
num = randint(1, 6)
print(num, '点')
sum1 += num
return sum1
print('第3题:', play_dice(2))
- 编写一个函数,交换指定字典的key和value。
如:{'a':1, 'b':2, 'c':3} ---> {1:'a', 2:'b', 3:'c'}
def change_num():
print(key1,value1 = value1,key1)
课条:
def change_key_value(dict1:dict):
for key in dict1.copy():
# 通过key拿到值
value = dict1[key]
# 删除原来的键值对
del dict1[key]
# 将值作为key,键作为value,添加键值对
dict1[value] = key
"""
copy: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
原:{ 1:'a', 2:'b', 3:'c', 4:'d' }
key = 'a' value=1 dict1[1]='a'
key = 'b' value=2 dict1[2]='b'
....
"""
dict11 = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
change_key_value(dict11)
print('第4题', dict11)
- 编写一个函数,提取指定字符串中的所有的字母,然后拼接在一起后打印出来
如:'12a&bc12d--' ---> 打印'abcd'
def my_alpha(str1):
str2 = {}
for x in str1:
x.isalpha()
str2.append(x)
课条:
def connect(str1:str):
mstr = ''
for char in str1:
if char.isalpha():
mstr += char
return mstr
print('第5题:', connect('12a&bchu23juj12d--'))
- 写一个函数,求多个数的平均值
def my_average(*nums):
print(sum(nums)/len(*nums))
课条:
def yt_mean(*nums):
# yt_sum的功能是求多个数的和
return sum(nums) / len(nums)
print('第6题:', yt_mean(1, 2, 3, 4, 5))
- 写一个函数,默认求10的阶层,也可以求其他数的阶层
def jie_cheng(x):
sum1 = 1
for x in range(1,x + 1):
sum1 *= x
return sum1
课条:
def yt_factorial(n=10):
sum1 = 1
for x in range(1, n+1):
sum1 *= x
return sum1
print('第7题:', yt_factorial(5))
print('第7题:', yt_factorial())
- 写一个函数,可以对多个数进行不同的运算
如: operation('+', 1, 2, 3) ---> 求 1+2+3的结果 operation('-', 10, 9) ---> 求 10-9的结果 operation('', 2, 4, 8, 10) ---> 求 24810的结构
def yt_operation(operator, *nums):
if operator == '+':
sum1 = 0
for num in nums:
sum1 += num
return sum1
elif operator == '-':
# 用第一个元素依次减后边的每一个数
sum1 = nums[0]
for index in range(1, len(nums)):
sum1 -= nums[index]
return sum1
elif operator == '*':
sum1 = 1
for num in nums:
sum1 *= num
return sum1
print('第8题:', yt_operation('+', 1, 2, 3, 4))
print('第8题:', yt_operation('*', 1, 2, 3, 4))
print('第8题:', yt_operation('-', 1, 2, 3, 4))
9.写一个函数,求指定列表中,指定的元素的个数
def my_element(list1):
sum1 = 0
list1 = [1, 2, 3, 4, 45, 2, 2]
ele = input('请输入元素')
for x in list1:
if x == ele:
sum1 += 1
continue
print(sum1)
课条:
def yt_count(list1:list, item):
count = 0
for x in list1:
if x == item:
count += 1
return count
print('第9题:', yt_count([1, 34, 56, 7, 1, 3, 1], 17))
10.写一个函数,获取指定列表中指定元素对应的下标(如果有多个,一起返回)
def my_index1(*lset1):
list1=[1,2,3,4,5,6,1,2,3]
num = input('请输入一个函数')
for x in list1:
课条 :
def yt_index(list1, item):
indexs = []
for index in range(len(list1)):
x = list1[index]
if x == item:
indexs.append(index)
return indexs
print('第10题', yt_index([1, 34, 56, 7, 1, 3, 1], 1))
网友评论