Write a program that asks a string to the user, and counts the number of time each character occurs in the string,output is a set
时间: 2023-05-13 09:06:52 浏览: 194
输出一个字符串中每个字符出现的次数
Sure, I can help you with that! Here's a Python program that does what you're asking for:
```
string = input("Please enter a string: ")
count = {}
for char in string:
if char in count:
count[char] += 1
else:
count[char] = 1
print("Character count:")
for char, num in count.items():
print(char, num)
```
This program prompts the user to enter a string, then creates a dictionary called `count` to keep track of how many times each character appears in the string. It loops through each character in the string, and if the character is already in the `count` dictionary, it increments the count by 1. If the character is not yet in the dictionary, it adds it with a count of 1. Finally, it prints out the character count for each character in the string.
阅读全文