+ 2

Izzy the Iguana Code Question Need help with my answer

Practice Problem: Your pet Iguana has run away, and you found it up in a tree! It will come down right away if you brought the right snacks, but if you don't have enough, you will have to wait. You need 10 total snack points to bring it down. Lettuce is worth 5, Carrot is worth 4, Mango is worth 9, and Cheeseburger is worth 0. ANSWER ATTEMPT: #FOOD PRICES Lettuce = 5 Carrot = 4 Mango = 9 Cheeseburger = 0 ############ foods = input() def foods_list_convert(x): a = list(x.split(" ")) return a foodList = foods_list_convert(foods) i = 0 while i < len(foodList): grub = 0 if foodList[i] == str(Lettuce): grub += Lettuce elif foodList[i] == str(Carrot): grub += Carrot elif foodList[i] == str(Mango): grub += Mango elif foodList[i] == str(Cheeseburger): grub += Cheeseburger i += 1 if grub > 10: print("Come on Down") else: print("Time to wait") ------------------- MY THOUGHTS: I think issue with the variable grub. Don't know how to take the info out of the while loop and use the new grub that's totaled up.

18th May 2022, 7:19 PM
Ademir
4 ответов
+ 2
Why not simplify with dictionary? snacks = {"Mango": 9, "Lettuce": 5, "Carrot": 4} points = 0 inv = input().split() for x in inv: points += int(snacks.get(x, 0)) if points >= 10: print("Come on Down!") else: print("Time to wait")
27th Sep 2022, 1:02 PM
Eugene Teh
Eugene Teh - avatar
0
str(Lettuce) : what you do mean by here? It returns str(5) => "5" just use "Lettuce". same for next all. Share code link by saving it...
18th May 2022, 7:32 PM
Jayakrishna 🇮🇳
0
Was able to fix with this: #FOOD PRICES Lettuce = 5 Carrot = 4 Mango = 9 Cheeseburger = 0 ############ grub = 0 foods = input() def foods_list_convert(x): a = list(x.split(" ")) return a foodList = foods_list_convert(foods) i = 0 while i < len(foodList): if foodList[i] == "Lettuce": grub += Lettuce elif foodList[i] == "Carrot": grub += Carrot elif foodList[i] == str(Mango): grub += Mango elif foodList[i] == str(Cheeseburger): grub += Cheeseburger i += 1 if grub > 10: print("Come on Down") else: print("Time to wait”) ---------------- I defined the var grub, outside of the while loop. Then changed as an example: str(Lettuce) to "Lettuce"
18th May 2022, 7:49 PM
Ademir
0
What about Mango, Cheeseburger? Do the same for those. use if grub >= 10 : >= instead of >
18th May 2022, 7:53 PM
Jayakrishna 🇮🇳