generate a random t-shirt size from xs to xl for my story in python

To generate a random t-shirt size from XS to XL, you can use the random module in Python. Here's an example using lists and the choice function:

main.py
import random

# create a list of t-shirt sizes
sizes = ["XS", "S", "M", "L", "XL"]

# generate a random t-shirt size
random_size = random.choice(sizes)

# print the result
print("Your random t-shirt size is:", random_size)
224 chars
11 lines

In this code, we create a list of t-shirt sizes from XS to XL. The random.choice() function randomly selects a size from that list, so the output will be a single size. Additionally, if you want to generate size with equal probability, you can use random.choices() function instead of random.choice() function.

main.py
import random

# create a list of t-shirt sizes
sizes = ["XS", "S", "M", "L", "XL"]

# generate a list of 10 random t-shirt sizes
random_sizes = random.choices(sizes, k=10)

# print the result
print("Your random t-shirt sizes are:", random_sizes)
247 chars
11 lines

In this example, we use random.choices() function instead of random.choice(). The k parameter is used to specify how many random sizes you want to generate in this example we generates a list of 10 sizes.

Additionally, you can add conditional operators to generate the sizes of medium or above.

main.py
import random

# create a list of t-shirt sizes
sizes = ["XS", "S", "M", "L", "XL"]

# generate a random t-shirt size medium or above
random_size = random.choice(sizes[2:])

# print the result
print("Your random t-shirt size is:", random_size)
244 chars
11 lines

In this code, we slice the list to exclude sizes smaller than medium, so random.choice() will only generate sizes M, L, and XL.

gistlibby LogSnag