How could I code this in python "try except" block? I have a dictionary (key and Values) the values are integer. In a "try except" block, I am to enter keys of my choice and the value will sum up. I am exiting with my last input. With "ctrl d" and then the program should give me the sum of the value of keys entered.
fruits = {
"mango": 4.00, "lemon": 2.00,
"orange": 3.00, "apple": 2.00,
"avocado": 2.50, "pineapple": 5.00,
"Watermelon": 6.50, "grapes": 4.5
}
price = 0
while True:
try:
n = input("Order: ")
if n in fruits:
price = fruits.get(n)
except(EOFError, KeyboardInterrupt):
print(f"Sum of inputs is: {price}")
break
That is my code above.
What is wrong with that code?
I am not getting what I want.
I do not want the program to prompt me before I quit with "control d". I want to quit when entering my last key. Then the program would print the sum of values for me.
CodePudding user response:
Don't use exception to handle control-flow in your code. Use conditions to leave the loop.
I do not want the program to prompt me before I quit with "control d". I want to quit when entering my last key.
is not possible - the program "loops too fast" for you to be able to quit it right when you sum up values - unless you force the program to sleep() for some time after you sum up the values:
from time import sleep
fruits = {"mango": 4.00, "lemon": 2.00, "orange": 3.00, "apple": 2.00,
"avocado": 2.50, "pineapple": 5.00, "watermelon": 6.50, "grapes": 4.5}
cost = 0
while True:
try:
n = input("Order: ")
if n not in fruits:
print("Invalid choice.")
# sum if in dict, else use 0
cost = fruits.get(n, 0)
sleep(2) # force a 2 second pause _every_ time to allow Ctrl-D/C
except KeyboardInterrupt:
break
print(f"Sum of inputs is: {cost}")
This is ugly though - the far better choice is to handle empty inputs as "I am done...":
fruits = {"mango": 4.00, "lemon": 2.00, "orange": 3.00, "apple": 2.00,
"avocado": 2.50, "pineapple": 5.00, "watermelon": 6.50, "grapes": 4.5}
cost = 0
print( *(f"{n} costs {p}" for n,p in fruits.items()), sep="\n")
while True:
n = input("Order: ")
if not n: # empty string is "Falsy" - same as most empty
break # iterables, 0, and some other values
if n not in fruits:
print("Invalid choice.")
cost = fruits.get(n, 0) # add 0 if not in dict
print(f"Sum of inputs is: {cost}")
to get
mango costs 4.0
lemon costs 2.0
orange costs 3.0
apple costs 2.0
avocado costs 2.5
pineapple costs 5.0
Watermelon costs 6.5
grapes costs 4.5
Order: lemon
Order: water # invalid choice
Invalid choice.
Order: grapes
Order: # empty input breaks
Sum of inputs is: 6.5 # 2 4.5
You should not use sum
as variable name, the name is taken by the build in sum()
function.
Exceptions are there to handle errors, not to steer your code's flow.
CodePudding user response:
You can use finally
to print the final price in the end
fruits = {
"mango": 4.00, "lemon": 2.00,
"orange": 3.00, "apple": 2.00,
"avocado": 2.50, "pineapple": 5.00,
"Watermelon": 6.50, "grapes": 4.5
}
price = 0
while True:
try:
n = input("Order: ")
price = fruits[n]
print("Item added.")
except:
print("Item is not in the list.")
finally:
print("Sum: " str(price))
and also you can print empty string after printing sum for better interface
fruits = {
"mango": 4.00, "lemon": 2.00,
"orange": 3.00, "apple": 2.00,
"avocado": 2.50, "pineapple": 5.00,
"Watermelon": 6.50, "grapes": 4.5
}
price = 0
while True:
try:
n = input("Order: ")
price = fruits[n]
print("Item added.")
except:
print("Item is not in the list.")
finally:
print("Sum: " str(price))
print("")
if you want to get the sum at the end you can print it outside of while
fruits = {
"mango": 4.00, "lemon": 2.00,
"orange": 3.00, "apple": 2.00,
"avocado": 2.50, "pineapple": 5.00,
"Watermelon": 6.50, "grapes": 4.5
}
price = 0
while True:
try:
n = input("Order: ")
price = fruits[n]
print("Item added.")
except:
print("Item is not in the list.")
break
print("Sum: " str(price))