鱼C论坛

 找回密码
 立即注册
查看: 2812|回复: 6

用ASC码看字符串结构程序求助

[复制链接]
发表于 2016-2-27 00:03:22 | 显示全部楼层 |阅读模式
5鱼币
def judge(*str1):
    n  = 0
    while n <= len(str1):
        num = 0
        strN = 0
        TabN = 0
        Och = 0
        m = 0
        for m in range(len(str1[n])):
            if  ord(str1[n][m]) in range (48,58):  #看有几个数字
                num += 1
            elif ord(str1[n][m]) in range(65,91) or ord(str1[n][m]) in range(97,123):  #有几个字母
                strN += 1
            elif ord(str1[n][m]) == 32:
                TabN += 1
            else:
                Och += 1   
        print('第 %d 个字符串共有:英文字母 %d 个,数字 %d 个,空格 %d 个,其他字符 %d 个'  % ( n, strN,num,TabN,Och ))
        n += 1
        continue
strA = input('请输入任意个字符串:')
judge(strA)


查看有几个字符,数字, 空格的,但是错了,
Traceback (most recent call last):
  File "H:/Python34/class 2.py", line 22, in <module>
    judge(strA)
  File "H:/Python34/class 2.py", line 9, in judge
    for m in range(len(str1[n])):
IndexError: tuple index out of range

最佳答案

查看完整内容

要想有多个元素,方法有3: 1.一次用多个input 2.for或while循环 3.把一个input结果拆分,这里用逗号
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-2-27 00:03:23 | 显示全部楼层
huaboshen 发表于 2016-2-27 14:15
我今天查看了str1的数据格式,用input()就只能得到1个元素的元祖。。。。

要想有多个元素,方法有3:
1.一次用多个input
  1. >>> str1 = input('Str1= '), input('\nStr2= ')
  2. Str1= qwe

  3. Str2= 123
  4. >>> str1
  5. ('qwe', '123')
复制代码

2.for或while循环
  1. >>> str1 = []
  2. >>> for i in range(2):
  3.         str1.append(input('string= '))

  4.        
  5. string= qwe
  6. string= 123
  7. >>> str1
  8. ['qwe', '123']
复制代码

3.把一个input结果拆分,这里用逗号
  1. >>> str1 = input('string = ')
  2. string = asd,123,!!!
  3. >>> list1 = str1.split(',')
  4. >>> list1
  5. ['asd', '123', '!!!']
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-2-27 10:46:59 | 显示全部楼层
是不是数字的范围错了?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-2-27 10:50:53 | 显示全部楼层
while n <= len(str1):判断有问题,如果只有一个参数,len(str1)为1,第一次循环无误,第二次循环时n=1,符合while的条件,但str1[1]是不存在的,出现错误。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-2-27 14:13:41 | 显示全部楼层

def judge(*str1):
    n  = 0
    print(len(str1),type(str1))
    while n < len(str1):
        num = 0
        strN = 0
        TabN = 0
        Och = 0
        m = 0
        for m in range(len(str1[n])):
            if  ord(str1[n][m]) in range (48,58):  #看有几个数字
                num += 1
            elif ord(str1[n][m]) in range(65,91) or ord(str1[n][m]) in range(97,123):  #有几个字母
                strN += 1
            elif ord(str1[n][m]) == 32:
                TabN += 1
            else:
                Och += 1   
        print('第 %d 个字符串共有:英文字母 %d 个,数字 %d 个,空格 %d 个,其他字符 %d 个'  % ( n, strN,num,TabN,Och ))
        n += 1
        continue
#strA = input('请输入任意个字符串:')
judge('adf12A','adfdf234')

谢谢,我改成这样就对了,但是又有个新问题,请问大神用input()函数赋值,最后得到的是什么格式的数据啊,我无论怎么输入得到的都只是1个元素的元祖,能赋值多个元素的列表,或者元祖么?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-2-27 14:15:23 | 显示全部楼层

我今天查看了str1的数据格式,用input()就只能得到1个元素的元祖。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-2-27 15:29:48 | 显示全部楼层
冬雪雪冬 发表于 2016-2-27 00:03
要想有多个元素,方法有3:
1.一次用多个input

太给力了,膜拜膜拜膜拜, 收益良多,良多!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-19 20:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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