Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the sum of a users input?

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

like image 925
Fazli Berisha Avatar asked Sep 28 '21 04:09

Fazli Berisha


People also ask

How do you find the sum of output 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.

How do I retrieve user input?

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.


Video Answer


4 Answers

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)
like image 102
PCM Avatar answered Oct 27 '22 14:10

PCM


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)
like image 25
Craze XD Avatar answered Oct 27 '22 16:10

Craze XD


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

like image 2
Behdad Abdollahi Moghadam Avatar answered Oct 27 '22 15:10

Behdad Abdollahi Moghadam


You might want to try out dict to simplify your if elifs and prints 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 elifstatements. After all your ifs just print the total: print('Total: ', sum)

like image 1
marcel h Avatar answered Oct 27 '22 16:10

marcel h