Skip to content
Advertisement

How can I find the sum of a users input?

print("Fazli's Vet Servicesn")
print("Exam: 50")
print("Vaccinations: 25")
print("Trim Nails: 5")
print("Bath: 20n")

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

Advertisement

Answer

Use dictionary –

print("Fazli's Vet Servicesn")
print("Exam: 50")
print("Vaccinations: 25")
print("Trim Nails: 5")
print("Bath: 20n")

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)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement