python 逻辑思维

本文记录了一次帮助测试部门同事用Python解决逻辑题的过程。题目涉及寻找符合特定条件的学号(清明同学)及其成绩提升情况。通过对给定字符串的分析,找出清明同学的学号、去年和今年的成绩以及成绩提升的百分点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近测试部门同事需要用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}")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值