Python面积计算器源代码

本程序通过一个while循环分两步计算图形面积,首先确认图形形状,然后根据不同的图形采用不同的面积公式进行计算,目前可支持矩形、正方形、三角形、梯形、圆形。

感兴趣的朋友还可以添加其它图形面积计算,也可对程序退出部分进行完善。

运行截图如下:
在这里插入图片描述

while True:

    shape = input("请输入要计算面积的图形:(矩形,正方形,三角形,梯形,圆形)")

    if (shape == "正方形") or (shape == "矩形"):

        a = int(input("请输入长:"))

        b = int(input("请输入宽:"))

        print(f"图形面积是{a}*{b}={a * b}")

    elif shape == "三角形":

        a = int(input("请输入底:"))

        b = int(input("请输入高:"))

        print(f"图形面积是({a}*{b})÷2={a * b / 2}")

    elif shape == "梯形":

        a = int(input("请输入上底:"))

        b = int(input("请输入下底:"))

        c = int(input("请输入高:"))

        print(f"图形面积是({a}+{b})×{c}÷2={(a + b) * c / 2}")

    elif shape == "圆形":

        r = int(input("请输入半径:"))

        print(f"图形面积是π{r}²={r ** 2 * 3.14}")

    else:

        print("其他图形暂不支持,欢迎添加!")
    更多Python源代码,欢迎微信关注Python代码大全

在这里插入图片描述

以下是一个简单的 Python 亲戚关系计算器源代码: ```python class Person: def __init__(self, name, gender, father=None, mother=None): self.name = name self.gender = gender self.father = father self.mother = mother def is_male(self): return self.gender == 'male' def is_female(self): return self.gender == 'female' def is_parent_of(self, person): return self == person.father or self == person.mother def is_sibling_of(self, person): return self != person and self.father == person.father and self.mother == person.mother def is_ancestor_of(self, person): if self == person: return True elif person.father and self.is_ancestor_of(person.father): return True elif person.mother and self.is_ancestor_of(person.mother): return True else: return False def is_descendant_of(self, person): return person.is_ancestor_of(self) def get_relationship(person1, person2): if person1.is_parent_of(person2): if person2.is_male(): return 'father' else: return 'mother' elif person1.is_sibling_of(person2): if person1.is_male(): return 'brother' else: return 'sister' elif person1.is_ancestor_of(person2): if person1.is_male(): return 'ancestor' else: return 'ancestor' elif person1.is_descendant_of(person2): if person1.is_male(): return 'descendant' else: return 'descendant' else: return 'unrelated' ``` 你可以使用这个代码计算两个人之间的亲戚关系。例如,如果你有一个名为 Alice 的女性祖母和一个名为 Bob 的男性表兄弟,你可以创建这些人的对象并调用 `get_relationship` 函数来计算他们之间的关系: ```python alice = Person('Alice', 'female') bob = Person('Bob', 'male') alice_daughter = Person('Alice\'s daughter', 'female', alice) bob_father = Person('Bob\'s father', 'male') alice_daughter_son = Person('Alice\'s daughter\'s son', 'male', father=None, mother=None) bob_father_son = Person('Bob\'s father\'s son', 'male', father=bob_father, mother=None) print(get_relationship(alice, bob)) # unrelated print(get_relationship(alice, alice_daughter)) # mother print(get_relationship(alice, alice_daughter_son)) # descendant print(get_relationship(alice, bob_father_son)) # unrelated ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Python代码大全

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值