design a progaram that accepts a number representing a class and then displays the class name
To design a program that accepts a number representing a class and displays the class name, you can follow these steps:
1. Define a dictionary that maps the class number to its corresponding class name. For example:
```python
class_map = {
1: 'Math',
2: 'Science',
3: 'History',
# Add more class numbers and names as needed
}
```
2. Prompt the user to enter the class number:
```python
class_number = int(input("Enter the class number: "))
```
3. Use a try-except block to handle invalid input. If the user enters a class number that is not present in the dictionary, display an error message:
```python
try:
class_name = class_map[class_number]
print("Class name:", class_name)
except KeyError:
print("Invalid class number!")
```
4. The program will now display the corresponding class name based on the entered class number.
Putting it all together, here's the complete program:
```python
class_map = {
1: 'Math',
2: 'Science',
3: 'History',
# Add more class numbers and names as needed
}
class_number = int(input("Enter the class number: "))
try:
class_name = class_map[class_number]
print("Class name:", class_name)
except KeyError:
print("Invalid class number!")
```
You can modify the `class_map` dictionary to include more class numbers and their respective names as needed.