首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Grok帮助字典

Grok帮助字典
EN

Stack Overflow用户
提问于 2014-07-26 05:05:00
回答 2查看 5.5K关注 0票数 1

我已经开始学习python了(就在几天前),我被困在了下面的代码中

代码语言:javascript
复制
# Enter your code for "Car colours" here.
gr = 'Cars that are green: '
si = 'Cars that are silver: '
re = 'Cars that are red: '
wh = 'Cars that are white: '
bl = 'Cars that are blue: '
print(gr, si, re, wh, bl, sep = '\n')
inp = input('Car: ')
while inp:
  Car = inp.split()
  line = input('Car: ')
  print(Car)

我完全不知道从这里到哪里去完成必须输出的代码,如下所示

代码语言:javascript
复制
Car: red
Car: white
Car: blue
Car: green
Car: white
Car: silver
Car: 
Cars that are green: 1
Cars that are silver: 1
Cars that are red: 1
Cars that are white: 2
Cars that are blue: 1

任何帮助将不胜感激,因为我找不到任何其他地方。我在为GROK做这段代码。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-26 05:25:33

它有助于使用字典:

代码语言:javascript
复制
count = {}
while True:
    color = input('Car: ')
    color = color.lower()
    if not color:
        break
    count.setdefault(color, 0)
    count[color] += 1
for color, n in count.items():
    print('Cars that are %s: %s' % (color, n))
票数 1
EN

Stack Overflow用户

发布于 2014-12-15 19:51:26

这个答案使用字典,并且仅限于Grok课程中在练习之前教授的示例代码。

代码语言:javascript
复制
car = {}
color = input("Car: ")

while color:
  if color not in car:
    car[color] = 1
  else:
    car[color] = car[color] + 1
  color = input("Car: ")
for x in car:
  print("Cars that are", x, ":", car[x])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24967738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档