Dictionary Car colours

Q:
You are curious about the most popular and least popular colours of cars and decide to write a program to calculate the frequency of car colours.

Your program should read in the colour of each car until a blank line is entered, and then print out (in any order) all the different colours of car with counts.

For example:

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

Here is another example:

Car: red
Car: white
Car: white
Car: red
Car: white
Car: white
Car: white
Car: 
Cars that are red: 2
Cars that are white: 5

A:

dic_colour1 = {}
dic_colour2 = {}
colour = []
car_colour = input("Car: ")

while car_colour:
  colour.append(car_colour)
  colour_num = colour.count(car_colour)
  car_sentence = "Cars that are " + car_colour+":"
  dic_colour1[car_sentence] = colour_num
  dic_colour2.update(dic_colour1)
  car_colour = input("Car: ")

for element in dic_colour2:
  print(element, dic_colour2[element])
  • list.append()
  • list.count()
  • create dictionaries in while loop
  • dictionary.update()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值