JavaScript
x
50
50
1
print("Fazli's Vet Servicesn")
2
print("Exam: 50")
3
print("Vaccinations: 25")
4
print("Trim Nails: 5")
5
print("Bath: 20n")
6
7
exam = "exam"
8
vaccinations = "vaccinations"
9
trim_nails = "trim nails"
10
bath = "bath"
11
none = "none"
12
13
exam_price = 50
14
vaccination_price = 25
15
trim_nails_price = 5
16
bath_price = 20
17
none_price = 0
18
19
first_service = input("Select first service:")
20
second_service = input("Select second service:")
21
22
print("nFazli's Vet Invoice")
23
24
if first_service == exam:
25
print("Service 1 - Exam: " + str(exam_price))
26
elif first_service == vaccinations:
27
print("Service 1 - Vaccinations: " + str(vaccination_price))
28
elif first_service == trim_nails:
29
print("Service 1 - Trim Nails: " + str(trim_nails_price))
30
elif first_service == bath:
31
print("Service 1 - Bath: " + str(bath_price))
32
elif first_service == none:
33
print("Service 1 - None " + str(none_price))
34
else:
35
print("Service 1 - None " + str(none_price))
36
37
38
if second_service == exam:
39
print("Service 2 - Exam: " + str(exam_price))
40
elif second_service == vaccinations:
41
print("Service 2 - Vaccinations: " + str(vaccination_price))
42
elif second_service == trim_nails:
43
print("Service 2 - Trim Nails: " + str(trim_nails_price))
44
elif second_service == bath:
45
print("Service 2 - Bath: " + str(bath_price))
46
elif second_service == none:
47
print("Service 2 - None " + str(none_price))
48
else:
49
print("Service 2 - None " + str(none_price))
50
Above is a code I have so far. It prints:
JavaScript
1
14
14
1
Fazli's Vet Services
2
3
Exam: 50
4
Vaccinations: 25
5
Trim Nails: 5
6
Bath: 20
7
8
Select first service: exam
9
Select second service: bath
10
11
Fazli's Vet Invoice
12
Service 1 - Exam: 50
13
Service 2 - Bath: 20
14
My goal is for the code to add up the two services and make a total price. My end goal should look like this:
JavaScript
1
15
15
1
Chanucey's Vet Services
2
3
Exam: 45
4
Vaccinations: 32
5
Trim Nails: 8
6
Bath: 15
7
8
Select first service: Exam
9
Select second service: none
10
11
Chauncey's Vet Invoice
12
Service 1 - Exam: 45
13
Service 2 - None: 0
14
Total: 45
15
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 –
JavaScript
1
30
30
1
print("Fazli's Vet Servicesn")
2
print("Exam: 50")
3
print("Vaccinations: 25")
4
print("Trim Nails: 5")
5
print("Bath: 20n")
6
7
dictionary = {'exam':50,'vaccinations':25,'trim nails':5,'bath':20,'none':0}
8
9
first_service = input("Select first service:").lower()
10
second_service = input("Select second service:").lower()
11
12
print("nFazli's Vet Invoice")
13
14
if first_service in dictionary:
15
price1 = int(dictionary[first_service])
16
print(f'Service 1 - {first_service.capitalize()} : {price1}')
17
else:
18
price1 = 0
19
print(f'Service 1 - None : 0')
20
21
if second_service in dictionary:
22
price2 = int(dictionary[second_service])
23
print(f'Service 1 - {second_service.capitalize()} : {price2}')
24
25
else:
26
price2 = 0
27
print(f'Service 1 - None : 0')
28
29
print('Total : ',price1+price2)
30