HW3Q3

 avatar
user_7676782
python
3 years ago
1.3 kB
3
Indexable
def list_to_3(lst):
    lst1 = []
    size = int(len(lst) / 3)
    # check if the length of
    # the list divisible by 3
    if len(lst) % 3 == 0:
        for r in range(size):
            x, y, z = lst[3 * r], lst[3 * r + 1], lst[3 * r + 2]
            lst1.append((x, y, z))
    else:
        for r in range(size + 1):
            if r == int(len(lst) / 3):
                # this if statement will be true if we need to add only 1 zero
                if 3 * (size + 1) - len(lst) == 1:
                    x, y, z = lst[3 * r], lst[3 * r + 1], 0
                # if not we add to end 2 zeros
                else:
                    x, y, z = lst[3 * r], 0, 0
            else:
                x, y, z = lst[3 * r], lst[3 * r + 1], lst[3 * r + 2]
            lst1.append((x, y, z))
    return lst1


def main():
    # define a list
    myList = []

    # prompt the user to enter a number
    while True:
        number = int(input("Enter a number (0 to quit): "))
        if number == 0:
            # if the number is 0, break out of the loop
            break
        else:
            # otherwise, append the number to the list
            myList.append(number)

    print(list_to_3(myList))


main()
Editor is loading...