-2

brand new to programming here, and having a lot of trouble with a homework problem. Please excuse my lack of understanding and knowledge. Any help would be greatly appreciated! Thank you!! I'm trying to output an if statement, when one of the items I'm calling is not in the list, and the function should print out that item.

def sushi(order):
    toppings = ['salmon', 'tuna', 'whitefish']
    for item in toppings:
        if 'salmon' or 'tuna' or 'whitefish' in toppings:
            print('all set')
            break
        if not item in toppings:
            print('Add to this', toppings.append(order))
            print('all set')

sushi(['salmon', 'tuna'])
sushi(['salmon', 'tuna', 'tempura'])

I want it to output:

all set
Add to this tempura
all set
vurmux
  • 9,420
  • 3
  • 25
  • 45
kimi315
  • 3
  • 1
  • 2
  • 4
    `if 'salmon' or 'tuna' or 'whitefish' in toppings` doesn't do what you think it does. It will always return `True` because `'salmon'` is considered Truthy. – Error - Syntactical Remorse Jun 19 '19 at 14:38
  • Also all those strings are always in the `toppings` because you initialise the list with them. – khelwood Jun 19 '19 at 14:40
  • 2
    Possible duplicate of [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – pault Jun 19 '19 at 14:41
  • Also you check `if not item in toppings` when `item` is the loop variable from toppings... – Tomerikoo Jun 19 '19 at 14:41
  • 1
    I think you have a decent amount of bugs in your code. Go through it with a debugger and check that everything happens as you expect. You need to fix your `if` and it appears you are using wrong variable names all over the place. You may also want to look into `all` and `any` keyword. – Error - Syntactical Remorse Jun 19 '19 at 14:47

2 Answers2

1

I believe what you are looking for is:

def sushi(order):
    toppings = ['salmon', 'tuna', 'whitefish']
    for item in order:
        if item not in toppings:
            print('Add to this', item)
    print("All set!")

>>> sushi(['salmon', 'tuna'])
All set!
>>> sushi(['salmon', 'tuna', 'tempura'])
Add to this tempura
All set!

The loop could be shortened by changing it to:

for item in [x for x in order if x not in toppings]:
    print('Add to this', item)

Your problems were:

1) for item in toppings:

I guess you wanted here order instead of toppings

2) if 'salmon' or 'tuna' or 'whitefish' in toppings:

here you probably wanted it to be: if 'salmon' in toppings or 'tuna' in toppings or 'whitefish' in toppings:. What you wrote is "if the string 'salmon' exists or the string 'tuna' exists or the string 'whitefish' is in toppings".

3) print('Add to this', toppings.append(order))

the method append does not return anything. maybe what you wanted is to add one line saying toppings.append(item) and then simply print item

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Thank you so much! why is 'item' able to be initialized when you call the function? How does it know to add tempura? And I'm sorry if this is a stupid question – kimi315 Jun 19 '19 at 14:59
  • @kimi315 the line `for item in order` means "go over each element of the list `order` and each iteration put this element in the variable `item`" – Tomerikoo Jun 19 '19 at 15:00
0

I think this does what you want

def sushi(order):
    toppings = ['salmon', 'tuna', 'whitefish']
    for item in order:
        if item in toppings:
            pass
        else:
            print('Add to this', item)
            toppings.append(item)
    print('all set')
>>> sushi(['salmon', 'tuna'])
all set

>>> sushi(['salmon', 'tuna', 'tempura'])
Add to this tempura
all set


Amit
  • 911
  • 8
  • 23