我有 2 个输入:
# The key is the available dates and the value is the price
a = {"g": 109192, "e": 116374, "o": 183368, "s": 162719}
# The dates that the user wants to take, this is going to be input by the user separeted by a space
b = ("g", "n", "e", "k", "s")
该程序必须告诉用户日期的总成本以及可用的日期。
输出:
388285
g e s
到目前为止我的代码:
import json
a=input("")
b=list(input().split(' '))
dic=json.dumps(a)
def citas(dic,b):
citas_disponibles=[]
suma=0
for dia in b:
if dia in a:
suma += a[dia]
citas_disponibles.append(dia)
return citas_disponibles
citas(dic,b)
def give_date_pricesum(a,b):
#assuming b is a list
available_dates = []
sum = 0
for date in b:
try:
sum = sum + a[date]
available_dates.append(date)
except:
print("date is not available")
return sum,available_dates
所以基本上在代码中,我遍历了用户想要的日期列表,并根据我们的价格字典检查它们。每当出现想要的日期时,我们都会将其添加到总和中,最后返回总和和可用日期。
如果您想为多个用户运行它并想要更新字典,那么您需要删除从用户选择中选择的条目。为此,您可以使用del dictionary[key]
格式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句