print("Fazli's Vet Services\n")
print("Exam: 50")
print("Vaccinations: 25")
print("Trim Nails: 5")
print("Bath: 20\n")
exam = "exam"
vaccinations = "vaccinations"
trim_nails = "trim nails"
bath = "bath"
none = "none"
exam_price = 50
vaccination_price = 25
trim_nails_price = 5
bath_price = 20
none_price = 0
first_service = input("Select first service:")
second_service = input("Select second service:")
print("\nFazli's Vet Invoice")
if first_service == exam:
print("Service 1 - Exam: " + str(exam_price))
elif first_service == vaccinations:
print("Service 1 - Vaccinations: " + str(vaccination_price))
elif first_service == trim_nails:
print("Service 1 - Trim Nails: " + str(trim_nails_price))
elif first_service == bath:
print("Service 1 - Bath: " + str(bath_price))
elif first_service == none:
print("Service 1 - None " + str(none_price))
else:
print("Service 1 - None " + str(none_price))
if second_service == exam:
print("Service 2 - Exam: " + str(exam_price))
elif second_service == vaccinations:
print("Service 2 - Vaccinations: " + str(vaccination_price))
elif second_service == trim_nails:
print("Service 2 - Trim Nails: " + str(trim_nails_price))
elif second_service == bath:
print("Service 2 - Bath: " + str(bath_price))
elif second_service == none:
print("Service 2 - None " + str(none_price))
else:
print("Service 2 - None " + str(none_price))
Above is a code I have so far. It prints:
Fazli's Vet Services
Exam: 50
Vaccinations: 25
Trim Nails: 5
Bath: 20
Select first service: exam
Select second service: bath
Fazli's Vet Invoice
Service 1 - Exam: 50
Service 2 - Bath: 20
My goal is for the code to add up the two services and make a total price. My end goal should look like this:
Chanucey's Vet Services
Exam: 45
Vaccinations: 32
Trim Nails: 8
Bath: 15
Select first service: Exam
Select second service: none
Chauncey's Vet Invoice
Service 1 - Exam: 45
Service 2 - None: 0
Total: 45
Notice how the code added both prices and made a "Total." Is there any way I can do this? I'm a beginner computer science major, so we aren't too far into Python.
ALL CODE IS IN PYTHON
sum() function in Python Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers.
The simplest way to obtain user input is by using the input() function. This function prompts the user for an input from the keyboard. Once the user has give the input and pressed Enter, the program flow continues.
Use dictionary -
print("Fazli's Vet Services\n")
print("Exam: 50")
print("Vaccinations: 25")
print("Trim Nails: 5")
print("Bath: 20\n")
dictionary = {'exam':50,'vaccinations':25,'trim nails':5,'bath':20,'none':0}
first_service = input("Select first service:").lower()
second_service = input("Select second service:").lower()
print("\nFazli's Vet Invoice")
if first_service in dictionary:
price1 = int(dictionary[first_service])
print(f'Service 1 - {first_service.capitalize()} : {price1}')
else:
price1 = 0
print(f'Service 1 - None : 0')
if second_service in dictionary:
price2 = int(dictionary[second_service])
print(f'Service 1 - {second_service.capitalize()} : {price2}')
else:
price2 = 0
print(f'Service 1 - None : 0')
print('Total : ',price1+price2)
Instead of using just: input('What you want to ask')
, use int(input('What you want to ask'))
first_service = int(input("Select first service:"))
second_service = int(input("Select second service:"))
Then you can simply run:
total = first_service + second_service
print("Total Cost: " + total)
I suggest you to do something like this:
Use Dictionary to store prices
Get, as many as, Services you want, not just two services
service_prices = {'exam':50,'vaccinations':25,'trim nails':5,'bath':20,'none':0}
serivces = [input().split()]
total_price = 0
for index, service in enumerate(services):
if service in service_prices:
print(f"Service {index} - {service}: {service_prices[service]}" )
total_price += exam_price
else:
print(f"Service {index} - None: {service_prices[none]}"
print(f"Total: {total_price}")
The advantage of this way is, clients can have services, as many as, they want.
Client should insert services, continuously and separate them with spaces; for example:
exam bath vaccinations
You might want to try out dict
to simplify your if elif
s and print
s using loops.
However, with your code for learning purposes you can try this:
sum = 0
if second_service == exam:
print("Service 2 - Exam: " + str(exam_price))
sum += exam_price
elif second_service == vaccinations:
print("Service 2 - Vaccinations: " + str(vaccination_price))
sum += vacination_price
...
in your first and second block of if elif
statements.
After all your if
s just print the total:
print('Total: ', sum)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With