如何通过列表 test_cases 中的每个数字进行 for 循环 运行?

How can I make my for loop run through every number in my list test_cases?

我在尝试 运行 我的代码 5 次时遇到问题,无法在标记为 test_cases 的列表中显示以下数字的因数。我怎样才能使用最简单的 python 函数而不 运行 出错?

def generate_factors(y):
    for i in range(1, y + 1): 
        if y % i == 0:
            print(i, end=' ')

test_cases = [20, 31, 140, 222, 517]

for one_tc in test_cases:
    print(f'The factors are: ')
    generate_factors(test_cases)

您将列表 test_cases 指定为 generate_factors 的输入,但它应该是整数 one_tc。试试这个:

def generate_factors(y):
    for i in range(1, y + 1): 
        if y % i == 0:
            print(i, end=' ')

test_cases = [20, 31, 140, 222, 517]

for one_tc in test_cases:
    print(f'The factors are: ')
    generate_factors(one_tc)
def generate_factors(y):
    for i in range(1, y + 1): 
        if y % i == 0:
            print(i, end='\n')
    
    

test_cases = [20, 31, 140, 222, 517]

for one_tc in test_cases:
    print(f'The factors of: ',one_tc)
    generate_factors(one_tc)