0

I have some code that takes overlapping elements from two lists and creates a new one with these elements. there are two functions which check for a valid input, however, the return statement returns no value to the main function. Can someone help me?

This is the code:

import random

import sys


def try_again():

    d = input("\nTry again? (y/n): ")
    if d == 'y':
        main_func()
    elif d != 'y' and d != 'n':
        print("Invalid entry")
        try_again()
    elif d == 'n':
        sys.exit()


def first_list_test(length_a, range_a):

    length_a = int(input("Enter the length of the first random list: "))
    range_a = int(input("Enter the range of the first random list: "))

    if length_a > range_a:
        print("The length of the list must be greater than the range for unique elements to generate")
        first_list_test(length_a, range_a)
    else:
        print("Length: " + str(length_a))
        print("Range: " + str(range_a))

    return length_a, range_a


def second_list_test(length_b, range_b):

    length_b = int(input("Enter the length of the second random list: "))
    range_b = int(input("Enter the range of the second random list: "))

    if length_b > range_b:
        print("The length of the list must be greater than the range for unique elements to generate")
        second_list_test(length_b, range_b)
    else:
        print("Length: " + str(length_b))
        print("Range: " + str(range_b))

    return length_b, range_b


def main_func():

    length_a = int()
    range_a = int()

    first_list_test(length_a, range_a)
    print(length_a)
    print(range_a)
    print("\n")

    length_b = int()
    range_b = int()

    second_list_test(length_b, range_b)
    print(length_b)
    print(range_b)
    print("\n")

    a = random.sample(range(1, range_a), length_a)
    a.sort()

    b = random.sample(range(1, range_b), length_b)
    b.sort()

    num = int()

    print(a, "\n")
    print(b, "\n")

    c = []
    d = []

    for num in a:

        if num in b:

            c.append(num)

    for test_num in c:

        if test_num not in d:

            d.append(test_num)

    d.sort()

    if len(d) == 0:
        print("No matches found")
    else:
        print("Overlapping elements: ")
        print(d)

    try_again()


main_func()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Which function do you think should return something but doesn't? Only half of them even try to return something. – jonrsharpe Aug 11 '21 at 14:03
  • 4
    Does this answer your question? [Why does my recursive function return None?](https://stackoverflow.com/questions/17778372/why-does-my-recursive-function-return-none) – jonrsharpe Aug 11 '21 at 14:04
  • 1
    When you `return` something you need to "catch" the returned values by assigning them to something. They don't just get pulled into the calling scope. So what you probably meant to do was something like this `length_a, range_a = first_list_test(length_a, range_a)` – Axe319 Aug 11 '21 at 14:07
  • 1
    Thank you, Axe319, this: length_a, range_a = first_list_test(length_a, range_a) solved the problem! – Robert Velciov Aug 11 '21 at 14:21

0 Answers0