|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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)
主要有2个问题。
input接收的是一个字符串,无法一次输入多个字符串。
*params不能作为变量名。
我修改了一下,你看看。
- 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('请输入字符串(可输入多个,用","隔开):')
- params = params.split(',')
- count(*params)
复制代码
|
|