Home > database >  How do I print the total from the users inputs?
How do I print the total from the users inputs?

Time:09-25

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))

How can I add the selected services up and have an overall total based on the users inputs? For example:

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

EDIT: According to my professors example, based on what the user selected, it prints out the total prices. I tried just using conditionals, but it seems I can't do it like that.

(I am a first-year computer science major! Forgive me if my code isn't the best looking.)

ALL CODE IS IN PYTHON

CodePudding user response:

Edit: After your edit it looks like you're looking for a sum() function? If that's the case and you want to just count the prices, then store the values in a list and afterwards call sum() on it:

prices = []
prices.append(int(input("Price 1")))
prices.append(int(input("Price 2")))
print(sum(prices))

or utilize a dictionary for your services:

prices = {}
prices["service 1"] = int(input("Price 1"))
prices["service 2"] = int(input("Price 2"))
print(sum(prices.values()))

which will also give you an option to count only specific services with easier than an access by an index (sum(mylist[idx] for idx in (0, 1)) vs sum(mydict[key] for key in ("service 1", "service 2")).

and you can even combine it (defaultdict is just a simple utility around the normal dict type):

from collections import defaultdict
mydict = defaultdict(list)
mydict["service 1"].append(int(input("Price 1 for Service 1")))
mydict["service 1"].append(int(input("Price 2 for Service 1")))
mydict["service 2"].append(int(input("Price 1 for Service 2")))
mydict["service 2"].append(int(input("Price 2 for Service 2")))
print(mydict, sum(mydict["service 1"]))

You can create a simple counter of the inputs by a decorator, therefore anytime you call input() or any other decorated function, you'll increment the counter and you'll be able to get it afterwards.

More on decorators you can find in the wiki.

The decorator itself will first check if the count attribute is present via getattr() (or use 0 as a default), adds 1 to it and store the counter in the wrapper function which you can then retrieve after the call.

def count(func):
    def wrapper(*args, **kwargs):
        wrapper.count = getattr(wrapper, "count", 0)   1
        return func(*args, **kwargs)
    return wrapper

@count
def hello(name):
    print(f"Hello, {name}!")

@count
def myinput(*args, **kwargs):
    return input(*args, **kwargs)

hello("Bob")
# Bob
hello("Anne")
# Anne
print(hello.count)
# 2

print(myinput("First input?"), myinput("Second input?"), myinput("Third?"))
print(myinput.count)
# 3
  • Related