---
title: "Setting an enum from user input"  
description: "Setting an enum from user input"  
author: "Revati S Misra"  
published: 2023-08-21  
updated: 2023-08-23  
canonical: https://www.mindstick.com/forum/159624/setting-an-enum-from-user-input  
category: "c#"  
tags: ["c#", "java", "enum"]  
reading_time: 2 minutes  

---

# Setting an enum from user input

Setting an [enum](https://www.mindstick.com/forum/159626/how-can-i-cast-a-string-to-an-enum) from [user input](https://www.mindstick.com/forum/33497/how-do-we-check-for-an-integer-user-input-in-java)

## Replies

### Reply by Aryan Kumar

Sure, here are the steps on how to set an enum from [user](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) [input](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java) in Python:

1. Create a variable to store the enum value.
2. Create a loop to get user input.
3. Use the `try-except` block to check if the user input is a valid enum value.
4. If the user input is a valid enum value, then set the enum value to the variable.
5. Otherwise, print an error message.

Here is an example of how to set an enum from user input in Python:

Python

```plaintext
enum MyEnum(str):
  Value1 = "VALUE1"
  Value2 = "VALUE2"
  Value3 = "VALUE3"

# Create a variable to store the enum value.
myEnum = None

# Create a loop to get user input.
while myEnum is None:
  print("Enter an enum value:")
  userInput = input()

  # Use the try-except block to check if the user input is a valid enum value.
  try:
    myEnum = MyEnum(userInput)
  except ValueError:
    print("The value is not a valid enum value.")

# Print the enum value.
print("The enum value is:", myEnum)
```

In this example, the `MyEnum` enum has three values: `Value1`, `Value2`, and `Value3`. The `myEnum` variable is a variable that stores the enum value. The `while` loop gets user input until the user enters a valid enum value. The `try-except` block checks if the user input is a valid enum value. If the user input is a valid enum value, then the `myEnum` variable is set to the user input. Otherwise, an error message is printed.


---

Original Source: https://www.mindstick.com/forum/159624/setting-an-enum-from-user-input

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
