gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
Prompt user for which videos to download choices = input("\nEnter video numbers to…
- User
Prompt user for which videos to download
codechoices = input("\nEnter video numbers to download (comma separated): ") indexes = [] for x in choices.split(","): x = x.strip() if x.isdigit(): idx = int(x) if 1 <= idx <= len(all_videos): indexes.append(idx - 1)Impove this part of code. Add support for dash symbol (range). For example 1,4,5-8,10-20. Also add duplicates fix
- Assistant
Certainly! Here's an improved version of your code snippet that supports both individual numbers and ranges (e.g.,
5-8), removes duplicates, and ignores invalid input gracefully:python - User
This code so long. Have you any ideas to make it shorter?
- Assistant
Sure! Here's a more concise version of the same logic, using a helper function and comprehensions:
pythondef parse_range(s, max_len): if '-' in s: start, end = s.split('-') if start.isdigit() and end.isdigit(): start, end = int(start), int(end) if start <= end: return [i - 1 for i in range(start, end + 1) if 1 <= i <= max_len] elif s.isdigit(): i = int(s) if 1 <= i <= max_len: return [i - 1] return [] choices = input("\nEnter video numbers to download (comma separated, support ranges e.g. 1,4,5-8): ") indexes = set(idx for part in choices.split(',') for idx in parse_range(part.strip(), len(all_videos))) indexes = sorted(indexes)