编程题:

Python第四章作业(初级) (educoder.net)

题一:无参无返回值函数

def print_hi_human():# 函数名用小写字母print("人类,你好!")if __name__ == '__main__':print_hi_human()

题二:无参有返回值函数

def say_hi_human():# 函数名用小写字母return "人类,你好!"if __name__ == '__main__':char = say_hi_human()print(char)

题三:有参有返回值函数

def say_hi_person(full_name):# 函数名用小写字母,函数名填空char = full_name + ",你好!"return charif __name__ == '__main__':#函数名作为print()函数的参数,输出say_hi_human()的返回值。输入的人名作为函数的参数person_name = input() char = say_hi_person(person_name)print(char) 

题四:多参函数

def say_hi_gender(full_name, gender):if(gender == '男'):print('尊敬的{}先生,欢迎来到火星!'.format(name))elif(gender == '女'):print('尊敬的{}女士,欢迎来到火星!'.format(name))else:print('尊敬的{}先生/女士,欢迎来到火星!'.format(name))if __name__ == '__main__':name = str(input())gender = str(input())say_hi_gender(name, gender)

题五:任意数量参数

def say_hi_multi_parameter(*name):# 括号里填上参数for i in name:print('{},你好!'.format(i))say_hi_multi_parameter('孟浩然')say_hi_multi_parameter('杜甫', '李白', '柳宗元', '李商隐')

题六:pow函数详解

x = float(input())y = float(input())num = pow(x,y)print(x**y)

题七:fabs() 函数

def fabs(x):"""返回x的绝对值"""# 补充你的代码return abs(x)if __name__ == '__main__':n = float(input())print(fabs(n))

题八:汽车迷

def Car(year,type1,a = "宝马"): # 括号里补充你的代码a = '这是一辆{:}年生产,型号是{:}的{:}牌汽车。'.format(year,type1,a)return a# 以下内容不要修改ls = input().split()# 根据空格切分输入字符串为列表print(Car(*ls)) # 调用函数,取列表中的全部数据做参数

题九:编写函数输出自除数

def sefDivisor(n):if '0' in str(n):return Falsefor i in str(n):if n % int(i) != 0:return Falseelse:return Truen = int(input())for m in range(1,n+1):if sefDivisor(m):print(m,end = ' ')

题十:二分法求平方根B

def sqrt_binary(x,g):low = 0high = x + 0.25m = (low + high) / 2while abs(m ** 2 - x) > g:if m ** 2 < x:low = melse:high = mm = (low + high) / 2print(f'{m:.8f}')print(f'{sqrt(x):.8f}')from math import sqrtx, g = input().split(',')sqrt_binary(float(x), float(g))

本人实力有限可能对一些地方解释和理解的不够清晰,可以自己尝试读代码,或者评论区指出错误,望海涵!

感谢大佬们的一键三连!感谢大佬们的一键三连!感谢大佬们的一键三连!