如何读取双for循环中的if语句

2024-04-23 13:57:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我的工作有问题代码。这些只是我程序的一个例子。在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");

Tags: thesample代码forifthatarelike
1条回答
网友
1楼 · 发布于 2024-04-23 13:57:43

假设你已经离开了电脑

方法:

1:列两张单子,一张上面有你喜欢的狗的名字,另一张上面有你不喜欢的狗的名字。你知道吗

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

相关问题 更多 >