如何打印用户输入的总数?

How do I print the total from the users inputs?

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

如何添加所选服务并根据用户输入得出总计?例如:

Chanucey 的兽医服务

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

编辑:根据我教授的示例,它会根据用户选择的内容打印出总价。我试过只使用条件,但似乎我不能那样做。

(我是计算机科学专业的一年级学生!如果我的代码不是最好看的,请原谅我。)

所有代码都在 PYTHON

编辑: 编辑后您似乎在寻找 函数?如果是这种情况,并且您只想计算 价格 ,则将这些值存储在列表中,然后在其上调用 sum() :

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

或利用 dictionary 为您提供服务:

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

这还将为您提供一个选项,只计算 特定的 服务,比通过索引访问更容易(sum(mylist[idx] for idx in (0, 1)) vs sum(mydict[key] for key in ("service 1", "service 2"))

你甚至可以组合它(defaultdict 只是围绕普通 dict 类型的一个简单实用程序):

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

您可以通过装饰器创建一个简单的输入计数器,因此无论何时您调用 input() 或任何其他装饰函数,您都会增加计数器,然后您就可以得到它。

有关装饰器的更多信息,您可以找到 in the wiki

装饰器本身将首先通过 getattr() (or use 0 as a default), adds 1 to it and store the counter 检查 count 属性是否存在,然后您可以在调用后检索该属性。

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