# 人狗大战(继承来完成)
# 人
# 狗
import random
class Model:
def __init__(self, name):
self.name = name
self.hp = 500
self.ad_max = 50
self.ad_min = 30
self.dodge = 0.2
def normal_attack(self, obj):
if random.random() <= self.dodge:
print(f"{obj.name} dodged {self.name}'s attack.")
else:
lose_hp = random.randint(self.ad_min, self.ad_max)
obj.hp -= lose_hp
print(f"{self.name} attack {obj.name}, {obj.name} lose {lose_hp}HP.")
class New_player(Model):
def __init__(self, name):
Model.__init__(self, name)
self.ad_max = 50
self.ad_min = 30
class New_dog(Model):
def __init__(self, name):
Model.__init__(self, name)
self.ad_max = 60
self.ad_min = 20
alex = New_player('alex')
xiaobai = New_dog('xiaobai')
def battle(obj_1, obj_2):
while True:
if obj_2.hp <= 0:
print(f'{obj_2.name} died.')
break
elif obj_1.hp <= 0:
print(f'{obj_1.name} died.')
break
obj_1.normal_attack(obj_2)
obj_2.normal_attack(obj_1)
battle(alex, xiaobai)
# 进阶
# 员工信息表
# select 查询这个文件
# select name,age where age>20
# select age,name,job where age > 20
# select age,name,job where age < 22
# select age,name,job where age = 22
# select age,name,job where name = 'alex'
# select age,name,job where job = 'IT'
# select age,name,job where phone like '133'
# select * where phone like '133'
# 文件处理 + input
# https://www.cnblogs.com/Eva-J/articles/7776508.html
import os
import re
BASE_PATH = os.path.dirname(__file__)
register_path = os.path.join(BASE_PATH, 'register')
def get_list():
info_list = []
with open('employee_info', encoding='utf-8', mode='r') as file_handler:
name_list = file_handler.readline().strip().split(',')
for line in file_handler:
new_dic = {}
temp_list = line.strip().split(',')
for i in range(len(name_list)):
new_dic.setdefault(name_list[i], temp_list[i])
info_list.append(new_dic)
return info_list
def search_age(symbol, age, info_list):
res_list = []
for i in info_list:
if symbol == 1 and int(i['age']) == int(age):
res_list.append(i)
elif symbol == 2 and int(i['age']) > int(age):
res_list.append(i)
elif symbol == 3 and int(i['age']) < int(age):
res_list.append(i)
return res_list
def search_job_name(mode, info, info_list):
res_list = []
for i in info_list:
if mode == 1 and i['job'] == info:
res_list.append(i)
elif mode == 2 and i['name'] == info:
res_list.append(i)
return res_list
def search_cellphone(num, info_list):
res_list = []
for i in info_list:
res = re.findall(num, i['phone'])
if res:
res_list.append(i)
return res_list
def run():
while True:
flag = False
string = input('PLS input ur instruction.\n')
info_list = get_list()
ret = re.search('select(.+?)where(.+)', string)
if not ret:
print('Illegal input.')
continue
if ret.group(1).strip() == '*':
print_line = 'id, name, age, phone, job'
else:
print_line = ret.group(1).strip()
filter_cond = ret.group(2).strip()
key_1 = re.search('age[ ]*(.)[ ]*(\d+)', filter_cond)
key_2 = re.search('name.*=[ ]*\'?(\w+)\'?', filter_cond)
key_3 = re.search('job.*=[ ]*\'?(\w+)\'?', filter_cond)
key_4 = re.search('phone.+?(\d+)', filter_cond)
res = []
if key_1:
flag = True
if key_1.group(1) == '=':
res = search_age(1, key_1.group(2), info_list)
elif key_1.group(1) == '>':
res = search_age(2, key_1.group(2), info_list)
elif key_1.group(1) == '<':
res = search_age(3, key_1.group(2), info_list)
elif key_2:
flag = True
res = search_job_name(2, key_2.group(1), info_list)
elif key_3:
flag = True
res = search_job_name(1, key_3.group(1), info_list)
elif key_4:
flag = True
res = search_cellphone(key_4.group(1), info_list)
print_list = print_line.split(',')
if flag == True:
for i in res:
for j in print_list:
key = j.strip()
if key in i:
print(i.get(key), end=' ')
print(' ')
break
if flag == False:
print('ILLEGAL INPUT.')
if __name__ == '__main__':
run()