Home>

The following questions were asked in the cram school assignments.

"Problem statement"
Create a list num from 1 to 30 using the comprehension and range. Create a list kisuu consisting of only odd numbers of 10 or less, with even numbers removed from the list num in a comprehension notation.
Then create an empty list guusuu.
Then declare the function tuika. Declare the variable abc with the argument a, add 1 to kisuu [a], make the odd number even, and return abc with return.
After that, declare a = 0, and while a is smaller than 7 in the while statement, put the one returned from the function tuika into the list of guusuu. However, when a becomes 5, the process is stopped with break.
Finally, display the lists kisuu and guusuu.

Where kisuu is displayed properly
guusuu remains empty.

Execution result
[1,3,5,7,9]
[]

Corresponding source code

Python 3

num = [x for x in range (1,31)]
kisuu = [y for y in num if ((y<= 10) and (y% 2 == 1))]
guusuu = []
def tuika (a):
    abc = kisuu [a] +1
    return abc
    a = 0
    while a<7:
        guusuu = tuika ()
        if a == 5:
            break
print (kisuu)
print (guusuu)
What I tried

I wrote it in the order of the question sentences, but for some reason only the list of guusuu remains empty.
Is something wrong?

Supplementary information (FW/tool version, etc.)

paiza

  • Answer # 1

    Only pointed out because it is a problem

    The block with a = 0 and while is indented too much and is included in the function definition, so it is not processed. Reduce your indentation by one.

    As pointed out by many others, the increment process of a in while is missing.

    -The process of adding the return value of the tuika function to the guusuu list is incorrect.

    The call to the tuika function has no arguments. Maybe you should specify a.

    The judgment of "when a becomes 5" is too late.