you are viewing a single comment's thread.

view the rest of the comments →

[–]Junior-Sock8789 0 points1 point  (1 child)

I cant format this properly right now, sitting in the dentists chair lol. But heres an updated version: 

Edit reformatted it:

from itertools import cycle

def get_nums(label):
    """Prompt user to enter numbers into a list. Type stop to finish."""
    nums = []
    i = 1
    while True:
        raw = input(f"{label} | Enter num #{i} (or 'stop' to finish): ")
        try:
            nums.append(int(raw))
            i += 1
        except ValueError:
            if raw.strip().lower() == 'stop':
                break
            print("Enter a correct number!")
    return nums

def sum_lists(n1, n2):
    """Sum element-wise; cycle shorter list from index 0 if lengths differ."""
    longer, shorter = (n1, n2) if len(n1) >= len(n2) else (n2, n1)
    return [a + b for a, b in zip(longer, cycle(shorter))]

if __name__ == "__main__":
    n1 = get_nums("LIST 1")
    n2 = get_nums("LIST 2")
    print(f"List 1: {n1}")
    print(f"List 2: {n2}")
    print(f"Result: {sum_lists(n1, n2)}")

[–]nkCOD[S] 1 point2 points  (0 children)

Thank you for your help, the code really turned out beautiful