30.0k views
3 votes
In this lab, you use what you have learned about parallel lists to complete a partially completed Python program. The program should either print the name and price for a coffee add-in from the Jumpin’ Jive Coffee Shop or it should print the message "Sorry, we do not carry that.".

2 Answers

8 votes

Final answer:

The question pertains to implementing a program with parallel lists in Python to match coffee add-in names with their prices, or display a message if the item is not found. The method includes using index() to retrieve data from lists and handling exceptions for non-existent items.

Step-by-step explanation:

The question is focused on using parallel lists in Python to manage and retrieve data. In Python, parallel lists refer to having multiple lists where each list holds related information and all lists are indexed in a synchronized manner. For example, you might have a list of product names and a corresponding parallel list with product prices. When a user queries for a specific product, such as a coffee add-in at the Jumpin’ Jive Coffee Shop, the program uses the index of that product name to find the exact price in the parallel price list. If the product is not found, the program returns a message indicating the item is not available.


To implement this, you can use the index() method to find the index of the given item in one list and then use that index to retrieve the corresponding item from another list. If the item is not found in the first list, catching the ValueError exception will allow the program to print the appropriate error message.


Here is a basic example of how the Python code could look:

product_names = ["Espresso", "Americano", "Latte"]
product_prices = [1.50, 2.00, 2.25]

try:
item_index = product_names.index("Latte")
print(f"{product_names[item_index]}: ${product_prices[item_index]}")
except ValueError:
print("Sorry, we do not carry that.")

Ensure to handle user input and match it against the list of product names accurately bearing in mind any case sensitivity or input variations.

User Prasath
by
5.5k points
5 votes

Final answer:

The student is asked to create a Python program using parallel lists to search for coffee add-in prices and to display a message if the item is not available.

Step-by-step explanation:

The student's question pertains to a Python programming task involving parallel lists. The program must search for a coffee add-in by name and provide the price or indicate if the item is not carried by the shop. To achieve this, the program will use two lists that work in parallel—one list contains names of the coffee add-ins, and the other contains their respective prices.

If an add-in name is provided, the program will look through the list of names. If found, it will print the corresponding price using the index from the names list to access the price list. If the add-in is not found, it will print an error message. This incorporates the concept of lists in Python programming, where indices of multiple lists are used in sync to maintain a relationship between different datasets (e.g., names and prices).

User Mentalist
by
5.3k points