鱼C论坛

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

[已解决]python作业求助’tuple函数的‘

[复制链接]
发表于 2022-12-4 23:25:54 | 显示全部楼层 |阅读模式

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

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

x
4cc7976fdc998782e7cf843a4a20f11.jpg
最佳答案
2022-12-5 09:37:39
  1. def posToIndex(s):
  2.     n = [ x for x in "12345678"]
  3.     dicta = {"a":0, "b":1, "c":2, "d":3, "e":4, "f":5, "g":6, "h":7,\
  4.              "A":0, "B":1, "C":2, "D":3, "E":4, "F":5, "G":6, "H":7}
  5.     ss = s.strip()
  6.     if len(ss)==2 or ss[1:(len(ss)-1)].isspace:
  7.         if ss[0] in n and ss[-1] in dicta.keys() :
  8.             x = n.index(ss[0])
  9.             y = dicta[ss[-1]]
  10.             return (max(x,y),min(x,y))
  11.         elif ss[0] in dicta.keys() and ss[-1] in n:
  12.             x = dicta[ss[0]]
  13.             y = n.index(ss[-1])
  14.             return (max(x,y),min(x,y))
  15.         raise ValueError
  16.     raise ValueError
复制代码
  1. posToIndex("C4")
  2. (3, 2)
  3. posToIndex("4c")
  4. (3, 2)
  5. posToIndex("  4  c")
  6. (3, 2)
  7. posToIndex("  x  c")
  8. Traceback (most recent call last):
  9.   File "<pyshell#49>", line 1, in <module>
  10.     posToIndex("  x  c")
  11.   File "<pyshell#45>", line 15, in posToIndex
  12.     raise ValueError
  13. ValueError
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-5 08:21:01 | 显示全部楼层
好歹截图放正啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-5 09:37:39 | 显示全部楼层    本楼为最佳答案   
  1. def posToIndex(s):
  2.     n = [ x for x in "12345678"]
  3.     dicta = {"a":0, "b":1, "c":2, "d":3, "e":4, "f":5, "g":6, "h":7,\
  4.              "A":0, "B":1, "C":2, "D":3, "E":4, "F":5, "G":6, "H":7}
  5.     ss = s.strip()
  6.     if len(ss)==2 or ss[1:(len(ss)-1)].isspace:
  7.         if ss[0] in n and ss[-1] in dicta.keys() :
  8.             x = n.index(ss[0])
  9.             y = dicta[ss[-1]]
  10.             return (max(x,y),min(x,y))
  11.         elif ss[0] in dicta.keys() and ss[-1] in n:
  12.             x = dicta[ss[0]]
  13.             y = n.index(ss[-1])
  14.             return (max(x,y),min(x,y))
  15.         raise ValueError
  16.     raise ValueError
复制代码
  1. posToIndex("C4")
  2. (3, 2)
  3. posToIndex("4c")
  4. (3, 2)
  5. posToIndex("  4  c")
  6. (3, 2)
  7. posToIndex("  x  c")
  8. Traceback (most recent call last):
  9.   File "<pyshell#49>", line 1, in <module>
  10.     posToIndex("  x  c")
  11.   File "<pyshell#45>", line 15, in posToIndex
  12.     raise ValueError
  13. ValueError
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-5 10:08:40 | 显示全部楼层
某度翻译:
  1. 任务3.将位置字符串转换为索引:pos Toindex(s)编写一个函数pos Toindex(s),该函数将字符串s作为参数,并返回一个元组(r,c),其中rand c对应于关联的板行和列位置的索引。字符串s应包含1,8中的一位整数和a,,h(或a,,h)中的一个字母,周围可能有空格。如果无法转换提供的字符串,则函数应引发aValueerror异常。示例:pos Toindex(C4)、postindex(4c)、postIndex(C4)都应返回元组(3,2)。但是,后置索引(x 9)或后置索引(c3 3“)应引起aValueerror第5页,共9页
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-5 21:38:36 | 显示全部楼层


治好了我多年的劲椎病(滑稽)

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 18:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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