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