鱼C论坛

 找回密码
 立即注册
查看: 936|回复: 4

[已解决]新手问题提问

[复制链接]
发表于 2020-5-18 02:42:21 | 显示全部楼层 |阅读模式

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

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

x
零基础入门学习Python 第十七讲课后题:
1. 编写一个函数,利用欧几里得算法(脑补链接)求最大公约数,例如 gcd(x, y) 返回值为参数 x 和参数 y 的最大公约数。

我写了一个为什么总是提示错误

之后是我的代码, 如何定义Python里的format, 有点不太明白,希望高手指点
最佳答案
2020-5-18 07:30:19
本帖最后由 Twilight6 于 2020-5-18 07:42 编辑

zsyyf 发表于 2020-5-18 04:00
自己解决了, 但还是想请教一下怎么看一个参数的format




format 是格式化的一种,它的格式化的格式是:
  1. ' {}  {}'.format(替换字段1,替换字段2)
复制代码



第一种使用情况:
在你 { } 中没有设置参数时,format默认顺序是和 ( ) 里的顺序一一对应来进行替换 如:
  1. name = 'Python'
  2. age = 18
  3. "hello, {}. you are {}?".format(name,age)
  4. >>>'hello, Python. you are 18?'
复制代码



第二种使用情况是通过位置参数索引:
  1. name = 'Python'
  2. age = 18
  3. "hello, {1}. you are {0}?".format(name,age)
  4. #  对应format后面的() 中的位置参数 可以不按顺序
  5. >>>'hello, 18. you are Python?'
复制代码



第三种情况使用关键字参数:
直接使用关键字方法:
  1. name = 'Python'
  2. age = 18
  3. "hello, {name1}. you are {age1}?".format(age1=age,name1=name)
  4. >>>'hello, Python. you are 18?'
复制代码

使用**字典关键字参数方法:
  1. person = {"name":"Python","age":18,"temp":"cool"}
  2. "hello, {temp}. you are {age}? {name}".format(**person)
  3. >>>'hello, cool. you are 18? Python'
复制代码



第四种情况关键字、位置参数混用(非常不建议这样用):
  1. name = 'Python'
  2. age = 18
  3. "hello, {name1}. you are {0}?".format(age,name1=name)
  4. >>>'hello, Python. you are 18?'
复制代码

但是如果混用要记住一点,formar后面的( )里的使用关键字参数的位置必须在使用位置参数的位置之后
正确例:
  1. print("hello, {name1}. you are {0}? {temp}".format(age,temp='cool',name1=name))
  2. # age是使用位置参数定位的所以要放再 temp、name1 使用关键字参数之前
复制代码

错误例:
  1. print("hello, {name1}. you are {0}? {temp}".format(temp='cool',age,name1=name))
  2. # age 在位置参数之后导致报错
复制代码



这边附带自己整理的笔记:
3种格式化用法
链接:http://note.youdao.com/noteshare ... C2183317BF11F4166E6


如果帮助到你了,记得给个最佳哦~

Capture1.PNG
Capture.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-18 04:00:46 | 显示全部楼层
自己解决了, 但还是想请教一下怎么看一个参数的format
Capture.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-18 07:30:19 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Twilight6 于 2020-5-18 07:42 编辑

zsyyf 发表于 2020-5-18 04:00
自己解决了, 但还是想请教一下怎么看一个参数的format




format 是格式化的一种,它的格式化的格式是:
  1. ' {}  {}'.format(替换字段1,替换字段2)
复制代码



第一种使用情况:
在你 { } 中没有设置参数时,format默认顺序是和 ( ) 里的顺序一一对应来进行替换 如:
  1. name = 'Python'
  2. age = 18
  3. "hello, {}. you are {}?".format(name,age)
  4. >>>'hello, Python. you are 18?'
复制代码



第二种使用情况是通过位置参数索引:
  1. name = 'Python'
  2. age = 18
  3. "hello, {1}. you are {0}?".format(name,age)
  4. #  对应format后面的() 中的位置参数 可以不按顺序
  5. >>>'hello, 18. you are Python?'
复制代码



第三种情况使用关键字参数:
直接使用关键字方法:
  1. name = 'Python'
  2. age = 18
  3. "hello, {name1}. you are {age1}?".format(age1=age,name1=name)
  4. >>>'hello, Python. you are 18?'
复制代码

使用**字典关键字参数方法:
  1. person = {"name":"Python","age":18,"temp":"cool"}
  2. "hello, {temp}. you are {age}? {name}".format(**person)
  3. >>>'hello, cool. you are 18? Python'
复制代码



第四种情况关键字、位置参数混用(非常不建议这样用):
  1. name = 'Python'
  2. age = 18
  3. "hello, {name1}. you are {0}?".format(age,name1=name)
  4. >>>'hello, Python. you are 18?'
复制代码

但是如果混用要记住一点,formar后面的( )里的使用关键字参数的位置必须在使用位置参数的位置之后
正确例:
  1. print("hello, {name1}. you are {0}? {temp}".format(age,temp='cool',name1=name))
  2. # age是使用位置参数定位的所以要放再 temp、name1 使用关键字参数之前
复制代码

错误例:
  1. print("hello, {name1}. you are {0}? {temp}".format(temp='cool',age,name1=name))
  2. # age 在位置参数之后导致报错
复制代码



这边附带自己整理的笔记:
3种格式化用法
链接:http://note.youdao.com/noteshare ... C2183317BF11F4166E6


如果帮助到你了,记得给个最佳哦~

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-5-18 08:04:55 | 显示全部楼层
这边建议用 f-string

还有,你的 gcd 这么写就可以了
  1. def gcd(x,y):
  2.     while y:
  3.         x,y=y,x%y
  4.     return x
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-19 01:36:12 | 显示全部楼层
Twilight6 发表于 2020-5-18 07:30
format 是格式化的一种,它的格式化的格式是:


第一种使用情况:

感谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-19 19:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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