鱼C论坛

 找回密码
 立即注册
查看: 1881|回复: 1

[已解决]19节练习

[复制链接]
发表于 2020-12-8 10:19:39 | 显示全部楼层 |阅读模式

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

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

x
题目为 统计出传入字符串参数的英文字母、空格、数字和其它字符的个数。

我的解答:
words = input('input your words here: ')

def azx(*words):
    letters = 0
    nums = 0
    blanks = 0
    others = 0

    for each in words:
        if each.isalpha() == True:
            letters += 1
            return letters
        elif each.isdigit() == True:
            nums += 1
            return nums
        elif each.isspace() == True:
            blanks += 1
            return blanks
        else:
            others += 1
            return others

print('字符串共有:英文字母','%d' % letters,'个,数字','%d' % nums,'个,空格','%d' % blanks,'个,其他字符','%d' % others,'个。')

我能问一下大佬们我这个错在哪里了吗?学艺不精,勿怪!谢谢大佬们了!
最佳答案
2020-12-8 10:33:19
帮你改了一下
  1. words = input('input your words here: ')

  2. def azx(words):
  3.     letters = 0
  4.     nums = 0
  5.     blanks = 0
  6.     others = 0

  7.     for each in words:
  8.         if each.isalpha() == True:
  9.             letters += 1
  10.         elif each.isdigit() == True:
  11.             nums += 1
  12.         elif each.isspace() == True:
  13.             blanks += 1
  14.         else:
  15.             others += 1
  16.     return letters, nums,blanks, others
  17. print('字符串共有:英文字母%d个,数字%d个,空格%d个,其他字符%d'%azx(words))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-8 10:33:19 | 显示全部楼层    本楼为最佳答案   
帮你改了一下
  1. words = input('input your words here: ')

  2. def azx(words):
  3.     letters = 0
  4.     nums = 0
  5.     blanks = 0
  6.     others = 0

  7.     for each in words:
  8.         if each.isalpha() == True:
  9.             letters += 1
  10.         elif each.isdigit() == True:
  11.             nums += 1
  12.         elif each.isspace() == True:
  13.             blanks += 1
  14.         else:
  15.             others += 1
  16.     return letters, nums,blanks, others
  17. print('字符串共有:英文字母%d个,数字%d个,空格%d个,其他字符%d'%azx(words))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-27 02:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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