0

I'm new to python, and while I was looking at a course, I had an idea of something I'm not finding a solution for :

proteins = ["venison", "tuna", "chicken"]
veggies = ["kale", "spinach", "tomatoes"]

for i in proteins:
    for j in veggies:
        if j == "spinach":
            print("Spinach?!? Feed it to the dog")
    print(i,j)
print(type(j))

Result :

venison kale
Spinach?!? Feed it to the dog
venison spinach
venison tomatoes
tuna kale
Spinach?!? Feed it to the dog
tuna spinach
tuna tomatoes
chicken kale
Spinach?!? Feed it to the dog
chicken spinach
chicken tomatoes

My intent is to, instead of printing "Venison Spinach..." and meat and spinach, really, is to have the sentence ("Spinach?!? Feed it to the dog") instead of printing the "menu".

The idea I have is then, Whenever j==spinach we print("Spinach?!? Feed it to the dog") and here we go to the next index before print(i,j).

How can I do this?

Hope my question makes sense! Thank you,

Barmar
  • 741,623
  • 53
  • 500
  • 612
m3.b
  • 447
  • 4
  • 15

4 Answers4

1
if j == 'Spinach':
  print(...)
  continue

OR

if j == 'Spinach':
   print("Feed it..")
else:
   print(i,j)
Chris Curvey
  • 9,738
  • 10
  • 48
  • 70
1

Use else in addition to if, like this:

proteins = ["venison", "tuna", "chicken"]
veggies = ["kale", "spinach", "tomatoes"]

for i in proteins:
    for j in veggies:
        if j == "spinach":
            print("Spinach?!? Feed it to the dog")
        else:
            print(i,j)
print(type(j))
Oliver
  • 94
  • 8
1
proteins = ["venison", "tuna", "chicken"]
veggies = ["kale", "spinach", "tomatoes"]

for i in proteins:
    for j in veggies:
        if j == "spinach":
            print("Spinach?!? Feed it to the dog")
        else:
            print(i,j)
0

Hahaha... as simple as else... Dumb me ^^' Why make easy when you can make complicated?

That being said, would there be a way to increment the index or element in some ways?

I tried

proteins = ["venison", "tuna", "chicken"]
veggies = ["kale", "spinach", "tomatoes"]

for i in proteins:
    for j in veggies:
        if j == "spinach":
            print("Spinach?!? Feed it to the dog")
            j=j+1
        print (I,j)

But it returns an error : TypeError: can only concatenate str (not "int") to str.

I understand I or j are not indexes of the list... so I wonder how I can do it.

Thank you everyone!

m3.b
  • 447
  • 4
  • 15