鱼C论坛

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

[已解决]Python 019讲 计算字符串字符数量

[复制链接]
发表于 2017-12-1 23:27:27 | 显示全部楼层 |阅读模式

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

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

x
  1. def count(*wordstr):
  2.     i = 0
  3.     z = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  4.     s = '0123456789'
  5.     k = ' '
  6.     a = 0
  7.     b = 0
  8.     c = 0
  9.     d = 0
  10.     x = 1
  11.     while x:
  12.         for y in range(len(wordstr[i])):
  13.             if y in z:
  14.                 a += 1
  15.             if y in s:
  16.                 b += 1
  17.             if y == k:
  18.                 c += 1
  19.         d = (len(wordstr[i]) - a - b - c)
  20.         x = 0   
  21.     return a
复制代码



上述是我刚刚完成一部分的代码       我想先运行一下看是否行得通       谁知得到了以下回复

  1. line 13, in count
  2.     if y in z:
  3. TypeError: 'in <string>' requires string as left operand, not int
复制代码



求解惑
最佳答案
2017-12-2 06:37:24
把for y in range(len(wordstr[i])):
改成
for y in wordstr:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-12-2 01:24:04 | 显示全部楼层
y是一个int型 z是str型
错误代码写得很详细了啊
TypeError: 'in <string>' requires string as left operand, not int
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-2 06:37:24 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
把for y in range(len(wordstr[i])):
改成
for y in wordstr:
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-2 11:23:26 | 显示全部楼层
yjsx86 发表于 2017-12-2 01:24
y是一个int型 z是str型
错误代码写得很详细了啊
TypeError: 'in ' requires string as left operand, not ...

晚上看昏了头
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-4 11:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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