编程题:
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 char
if __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 False
for i in str(n):
if n % int(i) != 0:
return False
else:
return True
n = int(input())
for m in range(1,n+1):
if sefDivisor(m):
print(m,end = ' ')
题十:二分法求平方根B
def sqrt_binary(x,g):
low = 0
high = x + 0.25
m = (low + high) / 2
while abs(m ** 2 - x) > g:
if m ** 2 < x:
low = m
else:
high = m
m = (low + high) / 2
print(f'{m:.8f}')
print(f'{sqrt(x):.8f}')
from math import sqrt
x, g = input().split(',')
sqrt_binary(float(x), float(g))