python - 在 python grok 中计算字符
问题描述
有一项任务,我必须输入名称,最后打印多少不同的字符和多少重复。
'你的程序应该读入多行输入,直到输入一个空行。然后它应该打印出有多少个独特的字符被命名,有多少被重复了。
这是我的结果:
角色:医生
角色:玫瑰
角色:罗里
角色:克拉拉
角色:K-9
角色:大师
角色:医生
角色:艾米
角色:
您命名了 8 个角色
您重复了 1 次
这是我的代码:
count = []
country = input('Character: ')
a = country.count(country)
b = 0
c = 0
while country:
count.append(country)
country = input('Character: ')
if a == country:
b = b + 1
else:
c = c + 1
c = c - b
count.sort()
print('You named',c,'character(s)')
print('You repeated',a,'time(s)')
应该说:
角色:医生
角色:玫瑰
角色:罗里
角色:克拉拉
角色:K-9
角色:大师
角色:医生
角色:艾米
角色:
您命名了 7 个角色。
您重复了 1 次。
解决方案
country = input('Character: ')
a = country.count(country)
b = 0
c = 0
while country:
count.append(country)
country = input('Character: ')
if country in count:
b = b + 1
else:
c = c + 1
count.sort()
print('You named',c,'character(s)')
print('You repeated',b,'time(s)')
结果:
字符:“AA”
字符:“BB”
字符:“CC”
字符:“BB”
字符:“”
('你命名',3,'字符')
('你重复',1,'时间( s)')
主要变化:
if country in count: //changed
c = c - b //removed
print('You named',c,'character(s)') //changed
print('You repeated',b,'time(s)') //changed
推荐阅读
- php - 切换PHP版本反映在终端客户端而不是服务器上
- r - R中一个因子的连续水平之间的对比
- sql - 有没有使用 .sql 脚本文件导入 .json 文件内容的好方法?
- android - ScaleGestureDetector.IOnScaleGestureListener C# Xamarin 的问题
- java - 如何简化两个相似方法的代码?
- sharepoint-online - 在 Power 应用和 Microsoft Forms 中自动生成唯一 ID
- python - 使用计算机视觉(OpenCV 和 Python)实时检测植物茎中的水分
- amazon-web-services - 如何从 SAM 中取消选中 API Gateway 方法中的“使用 Lambda 代理集成”?
- git - 本地 git 的 PyCharm 版本控制未提交
- python - 如何计算连续数列的实例数及其频率?