{}
See how a CS professor is using our compiler for class assignment.
Try Programiz PRO for Educators!
Learn DSA with step-by-step code visualization.
Try Programiz PRO for Educators!
run-icon
main.py
# Online Python compiler (interpreter) to run Python online. # Write Python 3 code in this online editor and run it. import random import string # ✅ **Level 1** # Generate a random 6-character user ID def random_user_id(): return ''.join(random.choices(string.ascii_letters + string.digits, k=6)) # Function to generate user-defined number of IDs and character length def user_id_gen_by_user(): length = int(input("Enter ID length: ")) count = int(input("Enter number of IDs to generate: ")) ids = [''.join(random.choices(string.ascii_letters + string.digits, k=length)) for _ in range(count)] return '\n'.join(ids) # ✅ **RGB Color Generator** def rgb_color_gen(): r = random.randint(0, 255) g = random.randint(0, 255) b = random.randint(0, 255) return f'rgb({r},{g},{b})' # ✅ **Level 2 – Hex and RGB Colors List Generator** # Generate list of hex colors def list_of_hexa_colors(n): colors = [] for _ in range(n): hex_color = '#' + ''.join(random.choices('0123456789abcdef', k=6)) colors.append(hex_color) return colors # Generate list of RGB colors def list_of_rgb_colors(n): return [rgb_color_gen() for _ in range(n)] # Generate hex or rgb colors dynamically def generate_colors(type, count): if type == 'hexa': return list_of_hexa_colors(count) elif type == 'rgb': return list_of_rgb_colors(count) else: return "Unsupported color format" # ✅ **Level 3 – Shuffle List and Unique Random Numbers** # Shuffle a list def shuffle_list(lst): shuffled = lst[:] random.shuffle(shuffled) return shuffled # Generate 7 unique random numbers between 0-9 def unique_random_numbers(): return random.sample(range(10), 7) # -------------------------- # Example usages: # Level 1 print("Random User ID:", random_user_id()) # Example: '1ee33d' print(user_id_gen_by_user()) # Try with 5 and 5 or 16 and 5 # RGB Color Generator print(rgb_color_gen()) # Example: rgb(125,244,255) # Level 2 - Generate Colors print(generate_colors('hexa', 3)) # Example: ['#a3e12f','#03ed55','#eb3d2b'] print(generate_colors('rgb', 3)) # Example: ['rgb(5, 55, 175)','rgb(50, 105, 100)','rgb(15, 26, 80)'] # Level 3 - Shuffle List and Unique Random Numbers print(shuffle_list([1, 2, 3, 4, 5])) # Example: [2, 5, 1, 3, 4] print(unique_random_numbers()) # Example: [2, 4, 6, 1, 9, 3, 5]
Output