函数——函数接收四个参数分别是:姓名,性别,年龄,学历。用户通过输入这四个内容,然后将这四个内容传入到函数中, 此函数接收到这四个内容,将内容追加到一个student msg文件中

def register(name,sex,age,edu):
    with open('student_msg.txt','a') as f1:
        f1.write(f'{name},{sex},{age},{edu}
')
n,s,a,e = input('输入姓名、性别、年龄、学历,并以逗号隔开').strip().replace('',',').split(',')
register(n,s,a,e)
  • input后面一定要跟strip(),固定搭配
  • split将字符串分割成一个列表
  • replace()把中文逗号替换成英文逗号,没有中文逗号,也不会报错
原文地址:https://www.cnblogs.com/cbbxxgc/p/12861396.html