鱼C论坛

 找回密码
 立即注册
查看: 26|回复: 3

[技术交流] 看看我的PYTHON

[复制链接]
发表于 2026-9-26 16:32:46 | 显示全部楼层 |阅读模式
购买主题 本主题需向作者支付 2 鱼币 才能浏览
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2026-9-26 16:33:03 | 显示全部楼层
鱼油你好,代码整体思路不错,但有几个地方会报错,帮你梳理一下:

1. times 被重复定义
后面的定义会覆盖前面的。第三次定义里:

  1. def times(s:list, n:int = 3) -> list:
  2.     return s * n
复制代码

s 没有默认值,所以执行到 print(times()) 时会报:
TypeError: times() missing 1 required positional argument: 's'
程序会在这里停止,后面的代码不会运行。

2. 类型注解不是默认值
s:list 只是提示 s 应该是列表,不会给它默认值。想有默认值要写 s: list = None 或 s: list = [] (可变默认参数要小心)。

3. 最后属性名写错了
应该是双下划线:

  1. print(times.__name__)
  2. print(times.__annotations__)
  3. print(exchange.__doc__)
复制代码

你写成了 _name_、_anotations_、_doc_,会报 AttributeError。另外 annotations 不是 anotations。

4. 建议
如果想分别测试 str、list、dict 版本,用不同函数名,例如 times_str、times_list、times_dict,否则前面的会被覆盖。

修改示例:

  1. def exchange(dollar, rate=6.32):
  2.     """汇率转换,美元 -> 人民币"""
  3.     return dollar * rate

  4. print(exchange(20))
  5. help(exchange)

  6. def times_str(s: str = "FishC", n: int = 3) -> str:
  7.     return s * n

  8. def times_list(s: list = None, n: int = 3) -> list:
  9.     if s is None:
  10.         s = []
  11.     return s * n

  12. def times_dict(s: dict, n: int = 3) -> list:
  13.     return list(s.keys()) * n

  14. print(times_str())
  15. print(times_list())
  16. print(times_dict({'A': 1, 'B': 2, 'C': 3}))

  17. print(times_str.__name__)
  18. print(times_str.__annotations__)
  19. print(exchange.__doc__)
复制代码


这样就能正常跑通并看到效果。类型注解只是提示,不影响实际运行。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2026-9-27 12:51:52 | 显示全部楼层
谢谢提醒
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2026-9-27 12:51:55 | 显示全部楼层


不客气~ 能帮到你就好,继续加油!有问题随时来问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-10-10 23:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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