6.python-练习定义函数

打印内容

"""
print("           登高")
print("        作者:杜甫")
print("风急天高猿啸哀,渚清沙白鸟飞回。")
print("无边落木萧萧下,不尽长江滚滚来。")
print("万里悲秋常作客,百年多病独登台。")
print("艰难苦恨繁霜鬓,潦倒新停浊酒杯。")
"""


def print_content(*content):
    """
        打印内容
    :param content: str类型 需要打印的内容
    :return:
    """
    for item in content:
        print(item)


print_content("           登高",
              "        作者:杜甫",
              "风急天高猿啸哀,渚清沙白鸟飞回。",
              "无边落木萧萧下,不尽长江滚滚来。",
              "万里悲秋常作客,百年多病独登台。",
              "艰难苦恨繁霜鬓,潦倒新停浊酒杯。")

获取句子成员

"""
    判断英文句子成分:I kiss you
    效果:
    请输入I kiss you的主语:I
    请输入I kiss you的谓语:kiss
    请输入I kiss you的宾语:you
    您输入的主语是:I,谓语是:kiss,宾语是:you
"""


# subject = input("请输入I kiss you的主语:")
# predicate = input("请输入I kiss you的谓语:")
# object = input("请输入I kiss you的宾语:")
# print("您输入的主语是:" + subject + ",谓语是:" + predicate + "宾语是,:" + object)


def get_sentence_constituent(sentence, constituent):
    """
        获取句子的成员
    :param sentence: str类型 句子
    :param constituent: str类型 成员称呼 如"主语,谓语,宾语"
    :return: str类型 句子成员
    """
    return input("请输入%s的%s:" % (sentence, constituent))


def connect_content(sentence):
    """
        拼接句子详情
    :param sentence: dict类型 句子成员字典
    :return: str类型 句子详情
    """
    result = "你输入的"
    for key, value in sentence.items():
        result += "%s是:%s " % (key, value)
    return result


subject = get_sentence_constituent("I miss you", "主语")
predicate = get_sentence_constituent("I miss you", "谓语")
the_object = get_sentence_constituent("I miss you", "宾语")

print(connect_content({"主语": subject, "谓语": predicate, "宾语": the_object}))

 

找回金额

# 在终端中输入商品单价,购买数量和支付金额.计算应该找回多少钱

# commodity_price = float(input("请输入商品单价:"))
# commodity_quantity = float(input("请输入购买数量:"))
# payment_amount = float(input("请输入支付金额:"))
# print("应找回:" + str(payment_amount - commodity_price * commodity_quantity))


def get_commodity_price():
    """
        获取商品单价
    :return: float类型 商品单价
    """
    return float(input("请输入商品单价:"))


def get_commodity_quantity():
    """
        获取购买数量
    :return: float类型 购买数量
    """
    return float(input("请输入购买数量:"))


def get_payment_amount():
    """
        回去支付金额
    :return: float类型 支付金额
    """
    return float(input("请输入支付金额:"))


def calculate_amount_recovered(price, quantity, amount):
    """
        计算找回金额
    :param price: float类型 价格
    :param quantity: float类型 数量
    :param amount: float类型 支付金额
    :return: float类型 找回金额
    """
    return amount - price * quantity


def explain_amount_recovered(explain, amount):
    """
        说明找回的金额
    :param explain:str类型 说明
    :param amount:float类型 找回金额
    :return: str类型 说明找回金额
    """
    return "%s%s" % (explain, amount)


commodity_price = get_commodity_price()
commodity_quantity = get_commodity_quantity()
payment_amount = get_payment_amount()

amount_recovered = calculate_amount_recovered(commodity_price, commodity_quantity, payment_amount)

print(explain_amount_recovered("应找回", amount_recovered))

治愈比例

"""
    在终端中输入一个疫情确诊人数再录入一个治愈人数,打印治愈比例
    格式:治愈比例为xx%
"""


# number_of_confirmed_cases = int(input("请输入确诊人数"))
# number_of_people_cured = int(input("请输入治愈人数"))
# print("治愈比例为" + str(number_of_people_cured / number_of_confirmed_cases * 100) + "%")


def get_number_people(content):
    """
        获取人数
    :param content:str类型 人数类别
    :return: int类型 人数
    """
    return int(input("请输入%s:" % content))


def calculate_cure_ratio(conform, cure):
    """
        计算治愈率
    :param conform: int类型 确诊人数
    :param cure: int类型 治愈人数
    :return: float类型 治愈比例
    """
    return cure / conform * 100


def explain_cure_ratio(explain, ratio):
    """
        说明治愈比例
    :param explain:str类型 说明
    :param ratio:float类型 比例
    :return: str类型 说明治愈比例
    """
    return "%s%.2f%%" % (explain, ratio)


conform_number = get_number_people("确诊人数")
cure_number = get_number_people("治愈人数")
cure_ratio = calculate_cure_ratio(conform_number, cure_number)
print(explain_cure_ratio("治愈比例为", cure_ratio))

换算重量

"""
    古代的秤,一斤十六两。
"""


# unit = 16
# sum_liang = int(input("请输入两数:"))
# jin = sum_liang // unit
# liang = sum_liang % unit
# print("一共是" + str(jin) + "斤零" + str(liang) + "两")

def get_weight(content):
    """
        获取重量
    :param content:str类型 重量单位
    :return: int类型 重量
    """
    return int(input("请输入%s:" % content))


def calculate_weight(weight):
    """
        换算重量
    :param weight: int类型 要换算的重量
    :return: tuple类型 元素int类型 换算后的重量
    """
    j = weight // 16
    lia = weight % 16
    return j, lia


def explain_weight(explain, weight):
    """
        说明重量
    :param explain: str类型 说明
    :param weight: dict类型 重量字典
    :return: str类型 说明重量
    """
    for key, value in weight.items():
        explain += "%s%s" % (value, key)
    return explain


total_liang = get_weight("总两数")
jin, liang = calculate_weight(total_liang)
print(explain_weight("换算后是", {"": jin, "": liang}))

求加速度

"""
    匀变速直线运动的速度与位移公式:
    位移 = 初速度 × 时间 + 加速度 * 时间的平方 / 2
    已知(在终端中录入):位移、时间、初速度
"""


# x = int(input("请输入位移:"))
# v0 = int(input("请输入初速度:"))
# t = int(input("请输入时间:"))
# v = (x - v0 * t) * 2 / (t ** 2)
# print("加速度:" + str(v))

def get_movement_data(content):
    """
        获取匀速直线运动数据
    :param content: str类型 数据名称
    :return: int类型 匀速直线运动数据
    """
    return int(input("请输入%s" % content))


def calculated_acceleration(move, start, time):
    """
        计算加速度
    :param move: int类型 位移
    :param start: int类型 初速度
    :param time: int类型 时间
    :return: float类型 加速度
    """
    acceleration = (move - start * time) * 2 / (time ** 2)
    return acceleration


def explain_acceleration(explain, acceleration):
    """
        说明加速度
    :param explain: str类型 说明
    :param acceleration: float类型 加速度
    :return: str类型 说明加速度
    """
    return "%s%.2f" % (explain, acceleration)


x = get_movement_data("位移")
v0 = get_movement_data("初速度")
t = get_movement_data("时间")

v = calculated_acceleration(x, v0, t)
print(explain_acceleration("匀加速直线运动的加速度是", v))

相加和

"""
    例如:录入1234,打印1+2+3+4结果
    效果:
        请输入四位整数:1234
        结果是:10
"""


# num = int(input("请输入四位数的整数:"))
# thousands = num // 1000
# hundreds = num // 100 % 10
# tens = num // 10 % 10
# single_digit = num % 10
# print("各个位数相加的和为:" + str(thousands + hundreds + tens + single_digit))

def get_integer():
    """
        获取整数
    :return: str类型 整数
    """
    number = input("请输入整数:")
    return number


def calculated_sum(integer):
    """
        计算各位相加和
    :param integer:str类型 整数字符串
    :return: int类型 和
    """
    result = 0
    for item in integer:
        result += int(item)
    return result


def explain_total(explain, total):
    """
        说明相加总和
    :param explain: str类型 说明
    :param total: int类型 总和
    :return: str类型 说明相加总和
    """
    return "%s%s" % (explain, total)


num = get_integer()
total_num = calculated_sum(num)
print(explain_total("各个位数相加的和是", total_num))

 

posted @ 2022-12-08 18:59  跃动指尖  阅读(68)  评论(0)    收藏  举报