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

问题描述 投票:-2回答:1

我的代码有问题。这些只是我程序的一个例子。我在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");
python python-3.x
1个回答
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)

OUTPUT:

The dogs that I like are
bulldogs
poodle
beagle
pug


The dogs that I don't like are
boxer
rottweiler
bullterrier
sheltie
© www.soinside.com 2019 - 2024. All rights reserved.