Python home Practice Test 2 Latest Update 2025-
2026 Questions and 100% Verified Correct
Answers Guaranteed A+
Create a solution that accepts a string input representing a grocery store item and an
integer input identifying the number of items purchased on a recent visit. The following
dictionary purchase lists available items as the key with the cost per item as the value.
purchase = {"bananas": 1.85, "steak": 19.99, "cookies": 4.52, "celery": 2.81, "milk": 4.34}
Additionally,
If fewer than ten items are purchased, the price is the full cost per item.
If between ten and twenty items (inclusive) are purchased, the purchase gets a 5%
discount.
If twenty-one or more items are purchased, the purchase gets a 10% discount.
Output the chosen item and total cost of the purchase to two decimal places.
The solution output should be in the format
item_purchased $total_purchase_cost -CORRECT ANSWER: item_purchased =
input().lower()
num_items = int(input())
if item_purchased in purchase:
total_purchase_cost = purchase[item_purchased]* num_items
if 10 <= num_items <= 20:
total_purchase_cost = total_purchase_cost - (total_purchase_cost*.05)
elif num_items >= 21:
total_purchase_cost = total_purchase_cost - (total_purchase_cost*.10)
print(f"{item_purchased} ${total_purchase_cost:.2f}")
, Create a solution that accepts an input identifying the name of a CSV file, for example,
"input1.csv". Each file contains two rows of comma-separated values. Import the built-in
module csv and use its open() function and reader() method to create a dictionary of
key:value pairs for each row of comma-separated values in the specified file. Output the
file contents as two dictionaries.
The solution output should be in the format
{'key': 'value', 'key': 'value', 'key': 'value'} {'key': 'value', 'key': 'value', 'key': 'value'} -
CORRECT ANSWER: import csv
input1 = input()
with open(input1, "r") as f:
data = csv.reader(f)
for row in data:
even = [row[i].strip() for i in range(0, len(row), 2)]
odd = [row[i].strip() for i in range(1, len(row), 2)]
pair = dict(zip(even, odd))
print(pair)
Create a solution that accepts an input identifying the name of a text file, for example,
"WordTextFile1.txt". Each text file contains three rows with one word per row. Using the
open() function and write() and read() methods, interact with the input text file to write a
new sentence string composed of the three existing words to the end of the file contents
on a new line. Output the new file contents.
The solution output should be in the format
word1 word2 word3 sentence -CORRECT ANSWER: file_name = input()
with open(file_name, 'r') as f:
word1 = f.readline().strip()
word2 = f.readline().strip()
word3 = f.readline().strip()
sentence = f"{word1} {word2} {word3}"