[Python] Replace every for loop with __init__, __iter__, and __next__ methods [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I need some help. The assignment is the following:


    Modify the attached Assignment 1.py code in such a way as to replace every for command in the code with appropriate __init__(), __iter__(), and __next__() commands where needed. The modified code should still perform identically to the unmodified one.

    Assignment 1.py:

    integer=eval(input("Enter a strictly positive integer: "))

    for iterations in range (0, integer):
    for a in range(0, integer):
    for b in range(0, integer-a):
    print(" ", end='')
    for b in range(0, 2*a+1):
    if b==0 or b==2*a or a==(integer//2):
    print("^", end='')
    else:
    print(" ", end='')
    print()

    print()

    integer=eval(input("Enter a strictly positive integer: "))

    print()

    for iterations in range (0, integer):
    for a in range(0, integer):
    print(" "*(integer-a) + " o"*(integer+a))
    for b in range(0, integer-1):
    print(" "*(b+2) + " o"*(integer*2-b-2))​

    Continue reading...

Compartilhe esta Página