如何在double for循环中读取if语句

时间:2019-02-15 06:11:56

标签: python python-3.x

我的代码有问题,这些只是我的程序示例。我在for循环和if语句中有一个for循环。我的目标是我要在下面打印输出

The dogs that i like are
bulldogs
poodle
beagle
pug

The dogs that i don't like are
boxer
rottweiler
bullterrier
sheltie

我尝试了这段代码,但似乎出了点问题。

sample = [['The dogs that i like are'], ["The dogs that i don't like are"]]
sample_2 = ['bulldogs', 'poodle', 'beagle','pug', 'boxer', 'rottweiler','bullterrier','sheltie']

i = 'pug'

for s in sample:
    print (sample)
    for s2 in sample_2:
        print (sample_2)
        if sample_2 == i:
            print ("\n");

1 个答案:

答案 0 :(得分:1)

假设您已经离开计算机

方法:

1:列出2张清单,一张列出您喜欢的所有狗的名字,另一张列出您不喜欢的狗的名字。

2:遍历列表以打印其名称。

liked_dogs = ['bulldogs', 'poodle', 'beagle', 'pug']    
not_liked_dogs = ['boxer', 'rottweiler', 'bullterrier', 'sheltie']


print("The dogs that I like are")
for dog in liked_dogs:
    print(dog)

print("\n")
print("The dogs that I don't like are")
for dog in not_liked_dogs:
    print(dog)

输出:

The dogs that I like are
bulldogs
poodle
beagle
pug


The dogs that I don't like are
boxer
rottweiler
bullterrier
sheltie