最近测试部门同事需要用python做一道题,他不会写找我帮忙写一下,顺手记了下来
清明同学的学号为首字母大写且只含有字母与数字组成的长度为8的字符串,
他的成绩书纯数字且小于100,并且得知清明同学今年的成绩比去年有所提升,
请从下列字符串中找到清明同学的学号,清明同学去年的成绩,今年的成绩,成绩提升的百分点,
并用字符串格式化显示出‘xx.x%’。如最终打印结果 清明的学号为:…,去年的成绩为:…,
今年的成绩为:…,成绩提升的百分点为:xx.x% 双击执行程序后可以查看到输出结果。
字符串:sma12 34,Ak‘12345,123snm,5 9,67,AK1234567,123,87,Ak234567
import random
import re
base_str = "sma12 34,Ak‘12345,123snm,5 9,67,AK1234567,123,87,Ak234567"
base_str = base_str.replace("‘","").replace(" ","").replace(",","")
# 学号
Student_number = ""
for _ in range(len(base_str)):
strs = random.choice(base_str)
Student_number += strs
if _ == 7:
tag = "".join(re.findall("(\d+)",Student_number))
if tag:
pass
else:
Student_number = Student_number[0:-1]
num = re.findall("(\d+)",base_str)[0]
Student_number += num
if Student_number[0].isupper():
pass
else:
isupper =None
for _ in base_str:
if _.isupper():
isupper = _
Student_number = isupper + Student_number[1:]
break
# 成绩
old_year = ""
new_year = ""
Num = "".join(re.findall("(\d+)",base_str))
for _ in range(2):
old_year += random.choice(Num)
new_year += random.choice(Num)
if int(new_year) > int(old_year):
pass
elif int(new_year )< int(old_year):
old_year,new_year = new_year,old_year
else:
old_num = None
new_num = None
while 1:
old_num = int(random.choice(Num))
new_num = int(random.choice(Num))
if new_num > old_num:
break
old_year = str(old_num) + str(old_year[1:])
new_year = str(new_num) + str(new_year[1:])
percentage = str(int((int(old_year) / int(new_year)) * 100)) + "%"
print(f"学号:{Student_number}");print(f"去年成绩:{old_year}");print(f"今年成绩:{new_year}")
print(f"提升的百分比:{percentage}")