问题-python-逆天bug-搜狗的标点符号修正导致的bug

本文介绍了如何使用Python定义学校成员类(包括教师和学生),以及添加、删除和显示成员信息的函数。作者还提到了一个关于中文逗号与英文逗号输入的意外问题。
摘要由CSDN通过智能技术生成

源代码:

#定义学校成员类
class SchoolMember:
 def __init__(self, name, age, sex):
   self.name = name
   self.age = age
   self.sex = sex
def get_info(self):
    return f"Name: {self.name}, Age: {self.age}, Sex: {self.sex}"
#定义教师类
class Teacher(SchoolMember):
 def __init__(self, name, age, sex, title, salary, prize):
  super().__init__(name, age, sex)
  self.title = title
  self.salary = salary
  self.prize = prize

 def get_total_income(self):
    return self.salary + self.prize

 def get_info(self):
    return f"Name: {self.name}, Age: {self.age}, Sex: {self.sex}, Title: {self.title}, Salary: {self.salary},prize:{self.prize} Total Income: {self.get_total_income()}"

#定义学生类
class Student(SchoolMember):
 def __init__(self, name, age, sex, dept, score):
  super().__init__(name, age, sex)
  self.dept = dept
  self.score = score

 def get_info(self):
     print(member)
     return f"Name: {self.name}, Age: {self.age}, Sex: {self.sex}, Department: {self.dept}, Score: {self.score}"

 #添加成员信息函数
# member=[]
def add_member():
     member_type = input("Add a teacher or student? ")
     if member_type.lower() == "teacher":
         string1=input("请依次输入姓名,年龄,性别,职位,工资,奖金,用中文逗号隔开:")
         array1 = string1.split(',')
         try:
             teacher = Teacher(array1[0], array1[1], array1[2], array1[3], array1[4], array1[5])
             members.append(teacher)
         except ValueError:
             print("输入错误")
         print("Teacher added successfully!")
     elif member_type.lower() == "student":
         string1 = input("请依次输入姓名,年龄,性别,所属系部、成绩用中文逗号隔开:")
         array1 = string1.split(',')
         print(array1)
         name = array1[0]
         age = array1[1]
         sex = array1[2]
         dept = array1[3]
         score = float(array1[4])
         student = Student(name, age, sex, dept, score)
         members.append(student)
         print("Student added successfully!")
     else:
         print("格式不对,请从新输入!")

#注销成员信息函数
def remove_member():
    name = input("Enter the name of the member you want to remove: ")
    for member in members:
        if member.name == name:
            members.remove(member)
            print(f"{member.name} removed successfully!")
            return
    print("Member not found!")

#主程序部分
members = []
while True:
    choice = input("请输入add或remove或info或end操作: ")
    if choice == "add":
        add_member()
    elif choice == "remove":
        remove_member()
    elif choice == "info":
        for member in members:
            print(member.get_info())
    elif choice == "end":
        break
    else:
        print("无效操作")

注意看这个代码是要用中文逗号隔离开,但是因为搜狗输入法的这个功能——

智能调整数字后标点,让本来输入的中文逗号变成了英文逗号,还好我第二次运行的时候就注意到了,不然这bug改一天都改不出来。输入的时候逗号会自动变化,发现了这个有点离谱的问题。分享一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值