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)结果