编写代码:随机生成40个字符,字符来源为A,B,C,D,统计每个字符的个数保存在字典中,并输出个数最多的字符
时间: 2023-08-03 22:08:28 浏览: 80
统计字符的个数
```python
import random
chars = ['A', 'B', 'C', 'D']
char_dict = {}
for i in range(40):
char = random.choice(chars)
if char in char_dict:
char_dict[char] += 1
else:
char_dict[char] = 1
print(char_dict)
max_char = max(char_dict, key=char_dict.get)
print(f"The character that appears most frequently is {max_char}, with {char_dict[max_char]} occurrences.")
```
输出示例:
```
{'A': 9, 'C': 10, 'D': 11, 'B': 10}
The character that appears most frequently is D, with 11 occurrences.
```
阅读全文