鱼C论坛

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

[已解决]19课课后练习求助

[复制链接]
发表于 2016-1-29 13:48:14 | 显示全部楼层 |阅读模式

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

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

x
19课课后练习动动手第1题,我想实现的效果是界面提示输入字符串(可输入多个),我输完后,在进行结果的输入,如何实现?我乱改了下代码(第二段),根本运行不起,求大神解答。

def count(*param):
    length = len(param)
    for i in range(length):
        letters = 0
        space = 0
        digit = 0
        others = 0
        for each in param[i]:
            if each.isalpha():
                letters += 1
            elif each.isdigit():
                digit += 1
            elif each == ' ':
                space += 1
            else:
                others += 1
        print('第 %d 个字符串共有:英文字母 %d 个,数字 %d 个,空格 %d 个,其他字符 %d 个。' % (i+1, letters, digit, space, others))
            
*params = input('请输入字符串(可输入多个):')
count(*params)

最佳答案
2016-1-29 18:47:05
主要有2个问题。
input接收的是一个字符串,无法一次输入多个字符串。
*params不能作为变量名。
我修改了一下,你看看。
  1. def count(*param):
  2.     length = len(param)
  3.     for i in range(length):
  4.         letters = 0
  5.         space = 0
  6.         digit = 0
  7.         others = 0
  8.         for each in param[i]:
  9.             if each.isalpha():
  10.                 letters += 1
  11.             elif each.isdigit():
  12.                 digit += 1
  13.             elif each == ' ':
  14.                 space += 1
  15.             else:
  16.                 others += 1
  17.         print('第 %d 个字符串共有:英文字母 %d 个,数字 %d 个,空格 %d 个,其他字符 %d 个。' % (i+1, letters, digit, space, others))
  18.             
  19. params = input('请输入字符串(可输入多个,用","隔开):')
  20. params = params.split(',')
  21. count(*params)
复制代码

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

使用道具 举报

发表于 2016-1-29 18:47:05 | 显示全部楼层    本楼为最佳答案   
主要有2个问题。
input接收的是一个字符串,无法一次输入多个字符串。
*params不能作为变量名。
我修改了一下,你看看。
  1. def count(*param):
  2.     length = len(param)
  3.     for i in range(length):
  4.         letters = 0
  5.         space = 0
  6.         digit = 0
  7.         others = 0
  8.         for each in param[i]:
  9.             if each.isalpha():
  10.                 letters += 1
  11.             elif each.isdigit():
  12.                 digit += 1
  13.             elif each == ' ':
  14.                 space += 1
  15.             else:
  16.                 others += 1
  17.         print('第 %d 个字符串共有:英文字母 %d 个,数字 %d 个,空格 %d 个,其他字符 %d 个。' % (i+1, letters, digit, space, others))
  18.             
  19. params = input('请输入字符串(可输入多个,用","隔开):')
  20. params = params.split(',')
  21. count(*params)
复制代码

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

使用道具 举报

发表于 2016-1-30 09:54:37 | 显示全部楼层
输入不对
*params 不可以直接作为参数名的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-2-1 14:51:46 | 显示全部楼层
冬雪雪冬 发表于 2016-1-29 18:47
主要有2个问题。
input接收的是一个字符串,无法一次输入多个字符串。
*params不能作为变量名。

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-19 10:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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