+ 2
Izzy the iguana issue
The test case no.4 keeps failing. Can anyone please, find where the mistake could be? Thank you! snacks = input().split(' ') points = 0 for i in snacks: if 'Lettuce' in snacks: points += 5 if 'Carrot' in snacks: points += 4 if 'Mango' in snacks: points += 9 if points >= 10: print('Come on Down!') else: print('Time to wait')
3 ответов
+ 6
I can't read the izzy the iguana question (no pro), but your for loop makes no sense:
Can you try it like this:
snacks = input().split(' ')
points = 0
for i in snacks:
if i == 'Lettuce':
points += 5
if i == 'Carrot':
points += 4
if i == 'Mango':
points += 9
if points >= 10:
print('Come on Down!')
else:
print('Time to wait')
+ 2
Atalantos the reason is that yours is checking if the snack is in the list. Hence, your test case will fail if there are duplicate snacks in the input.
Paul's solution checks if the list item is the snack and adds points accordingly.
Here's how using dictionary makes the code simpler:
snacks = {"Mango": 9, "Lettuce": 5, "Carrot": 4}
points = 0
inv = input().split()
#get function to retrieve the value of the snack in the inventory from the dictionary. If the snack is not inside the dictionary, the second argument in the get function returns a 0, adding no points.
for x in inv:
points += int(snacks.get(x, 0))
if points >= 10:
print("Come on Down!")
else:
print("Time to wait")
+ 1
Yup, that was it. Thank you very much Paul!