Faz*_*sha 5 python conditional-statements
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))
Run Code Online (Sandbox Code Playgroud)
上面是我到目前为止的代码。它打印:
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
Run Code Online (Sandbox Code Playgroud)
我的目标是让代码将两项服务相加并得出总价。我的最终目标应该是这样的:
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
Run Code Online (Sandbox Code Playgroud)
请注意代码如何添加两个价格并得出“总计”。我有什么办法可以做到这一点吗?我是一名计算机科学专业的初学者,所以我们对 Python 还不太了解。
所有代码都在 Python 中
使用字典 -
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
123 次 |
| 最近记录: |