我已经开始学习python了(就在几天前),我被困在了下面的代码中
# 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)我完全不知道从这里到哪里去完成必须输出的代码,如下所示
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做这段代码。
谢谢
发布于 2014-07-26 05:25:33
它有助于使用字典:
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))发布于 2014-12-15 19:51:26
这个答案使用字典,并且仅限于Grok课程中在练习之前教授的示例代码。
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])https://stackoverflow.com/questions/24967738
复制相似问题