python

 1.

(1)代码

# 创建空的密码文件
with open('password.txt', 'w') as password_file:
    pass


# 用户注册函数
def signup(username, password):
    with open('password.txt', 'a') as password_file:
        password_file.write(f"{username}:{password}\n")
    print("注册成功!")


# 用户登录函数
def login(username, password):
    with open('password.txt', 'r') as password_file:
        lines = password_file.readlines()
        for line in lines:
            stored_username, stored_password = line.strip().split(':')
            if username == stored_username and password == stored_password:
                return True
        return False


# 用户界面
while True:
    print("**********\n1.注册\n2.登录\n**********")
    choice = input("请输入序号选择功能: ")

    if choice == '1':
        username = input("请输入用户名: ")
        password = input("请输入密码: ")
        signup(username, password)
    elif choice == '2':
        username = input("请输入用户名: ")
        password = input("请输入密码: ")
        if login(username, password):
            print("登录成功!")
        else:
            print("登录失败,用户名或密码错误。")
    else:
        print("无效的选择,请重新输入。")

(2)结果

 2.

(1)代码

import math


class Shape:
    def __init__(self, center_x, center_y):
        self.center_x = center_x
        self.center_y = center_y

    def is_in(self, x, y):
        pass


class Rectangle(Shape):
    def __init__(self, center_x, center_y, width, height):
        super().__init__(center_x, center_y)
        self.width = width
        self.height = height

    def is_in(self, x, y):
        half_width = self.width / 2
        half_height = self.height / 2
        left_x = self.center_x - half_width
        right_x = self.center_x + half_width
        top_y = self.center_y - half_height
        bottom_y = self.center_y + half_height

        return left_x <= x <= right_x and top_y <= y <= bottom_y


class Circle(Shape):
    def __init__(self, center_x, center_y, radius):
        super().__init__(center_x, center_y)
        self.radius = radius

    def is_in(self, x, y):
        distance = math.sqrt((x - self.center_x) ** 2 + (y - self.center_y) ** 2)
        return distance <= self.radius


rectangle = Rectangle(0, 0, 6, 6)
circle = Circle(0, 0, 4)

print(rectangle.is_in(1, 1))
print(circle.is_in(4, 0))
print(rectangle.is_in(8, 0))
print(circle.is_in(3, 4))








(2)结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值