python实验3-1

1、编写函数求三个整数的最大值,函数原型为 def max3(a, b, c)

# 博主链接:https://blog.csdn.net/qq_45148277
# email:[email protected]
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2022/4/4 17:21
# 1、编写函数求三个整数的最大值,函数原型为 def max3(a, b, c)
def Max():
    print("请输入三个数:")
    a = int(input())
    b = int(input())
    c = int(input())
    maxx = -100000000000000000000
    if a > b:
        maxx = a
    else:
        maxx = b
    if maxx > c:
        return int(maxx)
    else:
        maxx = c
        return int(maxx)


n = Max()
print("最大值为:", n)

2、编写函数main()接收一个包含若干整数的列表参数lst,要求返回其中大于8的偶数组成的新列表,如果不存在就返回空列表。如果接收到的参数lst不是列表或者列表中不都是整数,就返回‘数据不符合要求’。

# 博主链接:https://blog.csdn.net/qq_45148277
# email:[email protected]
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2022/4/4 17:21
A = 1
list_A = []
list_B = []

# [1,2,3,4,5,6,7,8,9]

def islst(lst):
    cnt = 0
    t = True
    for i in lst:
        if type(i) != type(A):
            t = False
    if(isinstance(lst, list) and t):
        for i in lst:
            if(i % 2 == 0 and i > 8):
                list_B.insert(cnt, i)
                cnt += 1
        if cnt > 0:
            return list_B
        else:
            return list_A
    else:
        return "数据不符合要求"

lst = eval(input())
# print(lst)
print(islst(lst))

3、函数 main()接受3个分别表示年、月、日的正整数year,month,day,要求返回表示year年month月day日是周几的整数,1表示周一,2表示周二,以此类推,7表示周日。例如main(2020,10,5)返回1.可导入必要的标准库。

# 博主链接:https://blog.csdn.net/qq_45148277
# email:[email protected]
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2022/4/4 20:27
import calendar

def Time(y,m,d) :
    n = calendar.weekday(y,m,d)
    return n+1


y = int(input("输入年份:"))
m = int(input("输入月份:"))
d = int(input("输入日期:"))
print(Time(y,m,d))

4、编写函数,函数原型为 def f(n),求 f(x)的值。函数的定义如下图所示:

在这里插入图片描述

# 博主链接:https://blog.csdn.net/qq_45148277
# email:[email protected]
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2022/4/4 20:28
def fun(x):
    if x < 1:
        return x
    elif x >= 1 and x < 10:
        return 2 * x - 1
    else:
        return 3 * x - 11


x = int(input())
print(fun(x))

5、输入两个正整数 m 和 n(m<n),求 m 到 n 之间(包括 m 和 n)所有素数的和,要求定义并调用函数 is_prime(x)来判断 x 是否为素数(素数是除 1 以外只能被自身整除的自然数)。例如,输入 1 和 10,那么这两个数之间的素数有 2、3、5、7,其和是 17

输入:m n

输出:素数和

样例输入:1 10

样例输出:17

# 博主链接:https://blog.csdn.net/qq_45148277
# email:[email protected]
# 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
# 开发时间:2022/4/4 20:30


def is_prime(x):
    global sum
    flag = True
    if x == 1:
        flag = False
    for j in range(2, x):
        if x % j == 0:
            flag = False
            break
    if flag == True:
        sum += x


sum = 0
n = int(input())
m = int(input())
for i in range(n, m):
    is_prime(i)
print(sum)

猜你喜欢

转载自blog.csdn.net/qq_45148277/article/details/123970121
3-1