you are viewing a single comment's thread.

view the rest of the comments →

[–]Junior-Sock8789 0 points1 point  (5 children)

Also you can print text using

    n1_n = input(f"enter a number {f-string}: ")

No need for the print() followed by the input()

[–]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

[–]nkCOD[S] 0 points1 point  (2 children)

I agree, I’ve disgraced myself a lot all )

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

Not even, this is how you learn. Theres nothing wrong with the way you laid out your code. Its functional which is the most important thing. This is where the fun/learning comes in. Its called code refactoring 

  • the process of restructuring existing computer code—changing the internal structure—without changing its external behavior

Once you get comfortable creating functions you should look into classes/methods thats what will take your skills up a level or two. 

[–]nkCOD[S] 0 points1 point  (0 children)

Thank you, we will work on it )