我已经开始学习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做这个代码。
由于
答案 0 :(得分:1)
使用字典有帮助:
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 :(得分:0)
这个答案使用字典,并且仅限于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])