Create a dictionary with different prices for various fruits. Let’s say someone asks for a fruit. First, try and check if the fruit exists in your dictionary. If it does, print out its price. Otherwise (if there’s a KeyError), raise an exception saying, “We don’t have that fruit, sorry.”
Open a text file and read the first four lines.
Resources:
Class code: https://replit.com/@Shrav816/IntroToPython-Class13#main.py
Contact Info:
shravyassathish@gmail.com (mentor)
kingericwu@gmail.com (TA)
felixguo41@gmail.com (TA)
fruits = {"lemon": 1.58, "pineapple": 4.56, "orange": 2.58, "jackfruit": 5.00, "sstrawberry": 3.45, "banana":1.30, "watermelon": 4.50}
Fruit = input("Name a fruit:\t").lower()
try:
print(f"A {Fruit} is {fruits[Fruit]} dollars")
except KeyError:
print(f"This fruit, {Fruit} is currently not in stock.")
#2
file = open("stuff.txt", "r")
lines = []
for x in range(4):
lines.append(file.readline())
inventory = {"apples" : "$1", "bananas" : "$1.30", "blueberries" : "$4", "blackberries" : "$4", "raspberries" : "$4", "orangeberries" : "4", "ornages" : "2"}
fruit = input("Part 1: please enter a fruit to find the price of: ")
try:
print(f"{fruit} are {inventory[fruit]} \n\nPart 2:")
except KeyError:
print("We don't have that fruit, sorry\n\nPart 2:")
except:
print("Sorry, something went wrong... Please try again later\n\nPart 2:")
doc = open("C:/Christopher/New Text Document.txt", "r")
for i in range(4):
print(doc.readline())
#1
fruits = {"peach": 2.45, "pineapple": 4.50, "banana": 3.50, "dragonfruit": 5.00, "durian": 20.00, "grape": 0.05, "watermelon": 7.50}
userFruit = input("Name a fruit:\t").lower()
try:
print(f"A {userFruit} is {fruits[userFruit]} dollars")
except KeyError:
print(f"Unfortunately, {userFruit} is currently unavailable.")
#2
file = open("stuff.txt", "r")
lines = []
for x in range(4):
lines.append(file.readline())