Write a program that takes a string input from the user and counts the number of unique characters in the string. Note:The program should only count each unique character once, even if it appears multiple times i n the input string. The program should also ignore any whitespace characters or special characters t hat are not letters or numbers.
时间: 2024-02-14 19:09:07 浏览: 61
Sure, I can help you with that! Here's a Python program that takes a string input from the user and counts the number of unique characters in the string:
```python
def count_unique_characters(input_string):
unique_characters = set()
for char in input_string:
if char.isalnum():
unique_characters.add(char.lower())
return len(unique_characters)
user_input = input("Enter a string: ")
count = count_unique_characters(user_input)
print("Number of unique characters:", count)
```
Here, we define a function `count_unique_characters` that takes an input string as argument. We initialize an empty set `unique_characters` to keep track of all the unique characters in the input string. Then, we loop through each character in the input string and check if it is an alphanumeric character using the `isalnum()` method. If it is, we convert it to lowercase and add it to the set of unique characters. Finally, we return the length of the set, which gives us the number of unique characters in the input string.
To use this program, simply run it and enter a string when prompted. The program will then count the number of unique characters in the string and print the result.
阅读全文