Validate a Menu Choice from Input

Medium ⏱ 12 min 57% acceptance ★★★★☆ 4.2
Write a function select_option(raw, options) that simulates handling a user's typed menu choice. raw is the string typed by the user (as if returned from input()), and options is a list of valid string choices. If raw.strip() (case-insensitively) matches one of options, return the matching option in its original casing from options. Otherwise return "Invalid option".

Examples

Example 1
Input
select_option(" Yes ", ["Yes", "No"])
Output
'Yes'
Explanation

Trimmed input matches "Yes" case-sensitively after stripping.

Example 2
Input
select_option("no", ["Yes", "No"])
Output
'No'
Explanation

Case-insensitive match returns the canonical casing "No".

Example 3
Input
select_option("maybe", ["Yes", "No"])
Output
'Invalid option'
Explanation

Not one of the allowed options.

Constraints

  • options is a non-empty list of distinct strings.

Topics

Input & Outputinput validation

Companies

SwiggyZomatoUber

Hints

Hint 1

Strip raw first, then compare with .lower() against each option's .lower().

Hint 2

Return the item from options (not raw) so casing stays canonical.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.