为什么赢了';这不是Python3的循环工作吗?

为什么赢了';这不是Python3的循环工作吗?,python,python-3.x,for-loop,Python,Python 3.x,For Loop,我试图得到一个输出,它说: We won in *year x*!! All about the U!! 我希望列表中每年都重复这一点,因此输出将是: We won in 1983!! All about the U!! We won in 1987!! All about the U!! ect. repeat for each year. 我一直得到的是: We won in [1983, 1987, 1989, 1991, 2001]!! All about the U!!

我试图得到一个输出,它说:

We won in *year x*!!

All about the U!!
我希望列表中每年都重复这一点,因此输出将是:

We won in 1983!!

All about the U!!

We won in 1987!!

All about the U!!

ect. repeat for each year.
我一直得到的是:

We won in [1983, 1987, 1989, 1991, 2001]!!

All about the U!!

*repeated for the length of the list, 5 times*
下面是我尝试的代码:
我哪里出错了?

您想使用循环变量(
wewon
),而不是列表(
yearlist
):


wewon
将采用
年鉴的每个值

@John,请尝试以下代码:

在每次迭代中,您将获得分配给wewon的列表项,即一年,所以不要使用print()函数中的list

您可以在以下位置尝试以下代码:

✓ 输出:

We won in 1983!!
All about the U!!
We won in 1987!!
All about the U!!
We won in 1989!!
All about the U!!
We won in 1991!!
All about the U!!
We won in 2001!!
All about the U!!

这两种解决方案都很好,但我只是对iterables有一种不自然的喜爱

In [65]: yr = (i for i in ('1983', '1987', '1989', '1991', '2001'))    

In [66]: type(yr)    
Out[66]: generator    

In [67]: while True:    
    ...:     try:    
    ...:         print(f'We won in {next(yr)}!!\nAll about the U!!')    
    ...:     except StopIteration:    
    ...:         pass    
    ...:         break    
输出:
我们在1983年赢了
关于美国的一切
我们在1987年赢了
关于美国的一切
我们在1989年赢了
关于美国的一切
我们在1991年赢了
关于美国的一切
我们在2001年赢了

关于美国的一切

format(wewon)
这很有效,非常感谢:D
yearlist = [1983, 1987, 1989, 1991, 2001]

for wewon in yearlist:
    print("We won in {}!!".format(wewon))
    print("All about the U!!")
We won in 1983!!
All about the U!!
We won in 1987!!
All about the U!!
We won in 1989!!
All about the U!!
We won in 1991!!
All about the U!!
We won in 2001!!
All about the U!!
In [65]: yr = (i for i in ('1983', '1987', '1989', '1991', '2001'))    

In [66]: type(yr)    
Out[66]: generator    

In [67]: while True:    
    ...:     try:    
    ...:         print(f'We won in {next(yr)}!!\nAll about the U!!')    
    ...:     except StopIteration:    
    ...:         pass    
    ...:         break