鱼C论坛

 找回密码
 立即注册
查看: 3072|回复: 5

[已解决]求助!

[复制链接]
发表于 2022-10-22 12:58:50 | 显示全部楼层 |阅读模式

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

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

x
  1. person = int(input("请输入需要录入多少位学生的信息:"))
  2. student_dict = []  # 用来储存学生信息


  3. class Student:

  4.     def __init__(self, name, age, address):
  5.         self.name = name
  6.         self.age = age
  7.         self.address = address


  8. for temp in range(person):
  9.     print("当前录入第%d位学生的信息,总共需要录入%s位学生的信息" % (temp + 1, person))
  10.     name = str(input("请输入学生的姓名:"))
  11.     age = int(input("请输入学生的年龄:"))
  12.     address = str(input("请输入学生的家庭地址:"))
  13.     print(name,age,address)
  14.     exec(f"stu_{temp}=Student({name},{age},{address});student_dict.append(stu_{temp})")
  15.     print(student_dict)
复制代码


为什么执行不了,卡住好久了,我的想法是批量创建类对象来储存数据
然后再读取出来
最佳答案
2022-10-22 14:33:43
本帖最后由 阿奇_o 于 2022-10-22 14:38 编辑
gandixiwang 发表于 2022-10-22 14:11
如果我执行的话会报错,说我未定义,我不太明白是什么回事
如果按你那样写就不会报错


如果要用 exec 注意改一下 是:Student('{name}', '{age}', '{address}');     不是 Student({name},{age},{address});
前者在exec的作用下是 Student('小明', 18, 'xxx'),
后者在exec的作用下是 Student(小明, 18, xxx)   
看得出区别了吧~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-22 13:12:08 | 显示全部楼层
本帖最后由 suchocolate 于 2022-10-22 13:18 编辑
  1. ================= RESTART: C:/Users/Administrator/Desktop/1.py =================
  2. 请输入需要录入多少位学生的信息:2
  3. 当前录入第1位学生的信息,总共需要录入2位学生的信息
  4. 请输入学生的姓名:11
  5. 请输入学生的年龄:11
  6. 请输入学生的家庭地址:11
  7. 11 11 11
  8. [<__main__.Student object at 0x0000016BFAC4CA00>]
  9. 当前录入第2位学生的信息,总共需要录入2位学生的信息
  10. 请输入学生的姓名:22
  11. 请输入学生的年龄:22
  12. 请输入学生的家庭地址:22
  13. 22 22 22
  14. [<__main__.Student object at 0x0000016BFAC4CA00>, <__main__.Student object at 0x0000016BFA55A880>]
复制代码

运行正常

另外倒数第二句可以这样写:没必要用exec
  1. student_dict.append(Student(name,age,address))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-22 13:24:51 | 显示全部楼层
  1. person = int(input("请输入需要录入多少位学生的信息:"))
  2. student_dict = []  # 用来储存学生信息

  3. print("总共需要录入%s位学生的信息" % person)
  4. for temp in range(person):
  5.     print("当前录入第%d位学生的信息" % (temp + 1))
  6.     name = input("请输入学生的姓名:")
  7.     age = int(input("请输入学生的年龄:"))
  8.     address = input("请输入学生的家庭地址:")
  9.     student_dict . append([name , age , address])

  10. print(student_dict)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-22 13:37:42 | 显示全部楼层
3点几?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-22 14:11:43 | 显示全部楼层
suchocolate 发表于 2022-10-22 13:12
运行正常

另外倒数第二句可以这样写:没必要用exec

  1. 请输入需要录入多少位学生的信息:2
  2. 当前录入第1位学生的信息,总共需要录入2位学生的信息
  3. 请输入学生的姓名:小明
  4. 请输入学生的年龄:12
  5. 请输入学生的家庭地址:山东



  6.     exec(f"student_dict.append(Student({name},{age},{address}))")
  7.   File "<string>", line 1, in <module>
  8. NameError: name '小明' is not defined

  9. 进程已结束,退出代码1
复制代码

如果我执行的话会报错,说我未定义,我不太明白是什么回事
如果按你那样写就不会报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-22 14:33:43 | 显示全部楼层    本楼为最佳答案   
本帖最后由 阿奇_o 于 2022-10-22 14:38 编辑
gandixiwang 发表于 2022-10-22 14:11
如果我执行的话会报错,说我未定义,我不太明白是什么回事
如果按你那样写就不会报错


如果要用 exec 注意改一下 是:Student('{name}', '{age}', '{address}');     不是 Student({name},{age},{address});
前者在exec的作用下是 Student('小明', 18, 'xxx'),
后者在exec的作用下是 Student(小明, 18, xxx)   
看得出区别了吧~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 10:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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