You can download this code by clicking the button below.
This code is now available for download.
This function takes two lists as arguments and randomly selects an element from each list (if there are enough elements) and returns the sum of these two elements. If either parameter is not a list, the function will return an error message.
Technology Stack : random, list, isinstance, ValueError, sum
Code Type : Function
Code Difficulty : Intermediate
import random
def random_list_sum(arg1, arg2):
try:
list1 = arg1
list2 = arg2
if not isinstance(list1, list) or not isinstance(list2, list):
raise ValueError("Both arguments must be lists.")
return sum(random.sample(list1, k=random.randint(1, min(len(list1), len(list2)))))
except Exception as e:
return str(e)