0

Python Developer Unit 10 Practice Vending Machine Selection

products = ["Water", "Chocolate", "Chips", "Soda", "Sandwich"] try: choice_index = int(input()) except IndexError: print ('wrong index') print(choice_index) What am I doing wrong???

24th Sep 2024, 4:36 PM
Danielle Strahan
2 ответов
+ 3
You are not raising an exception in any case because you are not trying to index products using choice_index. Read the task again to find what to print out.
24th Sep 2024, 4:42 PM
Lisa
Lisa - avatar
+ 3
In this puzzle, you're goal is to: 1. Print the desired product 2. If the choice_index is invalid, print an error message. To do this, you need to attempt to print the desired product INSIDE the TRY section. Whatever you do inside the TRY section will either succeed or will cause an exception. products[99] will create an error. products[2] will return "Chips". So do the print statement inside the try section. Let the program determine if that's valid or an exception is needed. print(products[choice_index]) is probably what you need to use. There should be one print statement in the TRY section and another print statement in the EXCEPT statement. Below that, there should not be any further print statements. Everything should happen in the TRY and the EXCEPT blocks.
24th Sep 2024, 4:59 PM
Jerry Hobby
Jerry Hobby - avatar