鱼C论坛

 找回密码
 立即注册
查看: 2233|回复: 11

求问 以下代码 哪里出了问题???

[复制链接]
发表于 2020-2-7 14:13:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. def func(name,sex="男",age,education):
  2.     f1=open("D:\student_msg.txt","a")
  3.     f1.write(f"姓名:{name},性别:{sex},年龄:{age},学历:{education}\n")
  4.     f1.close()
  5.     f2=open("D:\student_msg.txt")
  6.     return f2.read()
  7. while 1:
  8.     content=input("请录入人员信息(如:张三,男,28,博士):")
  9.     if content.upper()!="Q":
  10.         information=content.split(",")
  11.         func(name=information[0],sex=information[1],age=information[2],education=information[3])
  12.     else:
  13.         print("已退出人员信息录入系统")
  14.         break
复制代码




SyntaxError: non-default argument follows default argument 这个是报错的信息
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-7 14:25:18 | 显示全部楼层
默认参数要放在最后
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-7 14:27:06 | 显示全部楼层
  1. def func(name,age,education,sex="男"):
  2.     f1=open("D:\student_msg.txt","a")
  3.     f1.write(f"姓名:{name},性别:{sex},年龄:{age},学历:{education}\n")
  4.     f1.close()
  5.     f2=open("D:\student_msg.txt")
  6.     return f2.read()
  7. while 1:
  8.     content=input("请录入人员信息(如:张三,男,28,博士):")
  9.     if content.upper()!="Q":
  10.         information=content.split(",")
  11.         func(information[0],information[2],information[3],sex=information[1])
  12.     else:
  13.         print("已退出人员信息录入系统")
  14.         break
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-7 14:52:04 | 显示全部楼层
  1. def func(sex="男",**kwargs):
  2.     f1=open("D:\student_msg.txt","a")
  3.     f1.write(f"姓名:{name},性别:{sex},年龄:{age},学历:{education}\n")
  4.     f1.close()
  5.     f2=open("D:\student_msg.txt")
  6.     return print(f2.read())
  7. while 1:
  8.     content=input("请录入人员信息(如:张三,男,28,博士):")
  9.     if content.upper()!="Q":
  10.         information=content.split(",")
  11.         func(name=information[0],sex=information[1],age=information[2],education=information[3])
  12.     else:
  13.         print("已退出人员信息录入系统")
  14.         break
  15. 那请问 这种情况下 怎么修改呢需要用*args和**kwargs
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-7 15:39:09 | 显示全部楼层
  1. def func(sex="男",**kwargs):
  2.     f1=open("D:\student_msg.txt","a")
  3.     f1.write(f"姓名:{kwargs['name']},性别:{sex},年龄:{kwargs['age']},学历:{kwargs['education']}\n")
  4.     f1.close()
  5.     f2=open("D:\student_msg.txt")
  6.     return print(f2.read())
  7. while 1:
  8.     content=input("请录入人员信息(如:张三,男,28,博士):")
  9.     if content.upper()!="Q":
  10.         information=content.split(",")
  11.         func(sex=information[1],name=information[0],age=information[2],education=information[3])
  12.     else:
  13.         print("已退出人员信息录入系统")
  14.         break
复制代码

你可以这么写,但是在提交前要判断是否有那四个信息。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-7 15:40:38 | 显示全部楼层
prophet-1 发表于 2020-2-7 15:39
你可以这么写,但是在提交前要判断是否有那四个信息。

我后边 也想到了 kwargs返回的是字典,但是 还是要报错。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-7 15:44:53 | 显示全部楼层
prophet-1 发表于 2020-2-7 15:39
你可以这么写,但是在提交前要判断是否有那四个信息。

为什么  sex 不能使用 kwargs['sex']  这么用回报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-7 15:47:00 | 显示全部楼层
奔跑的少年丶 发表于 2020-2-7 15:44
为什么  sex 不能使用 kwargs['sex']  这么用回报错

你的sex是直接赋值的,kwargs是后边多余的键值对。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-7 15:50:06 | 显示全部楼层
prophet-1 发表于 2020-2-7 15:47
你的sex是直接赋值的,kwargs是后边多余的键值对。

哦哦哦 对对 他是先将 sex默认值改了,sex和它对应的值不会进入到字典里
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-7 15:50:36 | 显示全部楼层
奔跑的少年丶 发表于 2020-2-7 15:44
为什么  sex 不能使用 kwargs['sex']  这么用回报错
  1. def func(**kwargs):
  2.     f1=open("D:\student_msg.txt","a")
  3.     f1.write(f"姓名:{kwargs['name']},性别:{kwargs['sex']},年龄:{kwargs['age']},学历:{kwargs['education']}\n")
  4.     f1.close()
  5.     f2=open("D:\student_msg.txt")
  6.     return print(f2.read())
  7. while 1:
  8.     content=input("请录入人员信息(如:张三,男,28,博士):")
  9.     if content.upper()!="Q":
  10.         information=content.split(",")
  11.         func(sex=information[1],name=information[0],age=information[2],education=information[3])
  12.     else:
  13.         print("已退出人员信息录入系统")
  14.         break
复制代码

你要想将所有的值都在kwargs中,你可以这样写。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-7 15:54:41 | 显示全部楼层
prophet-1 发表于 2020-2-7 15:50
你要想将所有的值都在kwargs中,你可以这样写。

但是 我想sex有默认值
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-7 15:57:34 | 显示全部楼层
奔跑的少年丶 发表于 2020-2-7 15:54
但是 我想sex有默认值

那就按之前的来。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-22 06:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表