Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算特定月份收到的罚款总额_Python - Fatal编程技术网

Python 计算特定月份收到的罚款总额

Python 计算特定月份收到的罚款总额,python,Python,我想计算特定月份收到的总罚款(将数据保存在列表中),但我不知道怎么做。有人能帮忙吗 bookcode = str(input('Enter Book Code To Return: ')) day = int(input('Enter Days Borrowed:')) if(0 <= day <= 5): print('You have returned your book successfully =)') else: penalty = (day-

我想计算特定月份收到的总罚款(将数据保存在列表中),但我不知道怎么做。有人能帮忙吗

bookcode = str(input('Enter Book Code To Return: ')) 
day = int(input('Enter Days Borrowed:'))

if(0 <= day <= 5):
        print('You have returned your book successfully =)') 
else:
    penalty = (day-5) * 1
    print('You have to pay RM%.2f ,%penalty)


print('\nYou have to pay RM','%.2f'%penalty)
bookcode=str(输入('输入要返回的图书代码:'))
day=int(输入('输入借用的天数:'))

如果(0您提供了代码,但没有提供它的用例(是否在每个月底运行?每次有人想要还书时?等等)

目前,让我们假设每当有人想要归还一本书时,我们都在运行这段代码

因此,要分解它:

  • 您的应用程序需要知道我们当前的日期
  • 它需要加载以前从某种形式的持久性存储(数据库或文件)提交的数据

  • 为了解决日期问题,我们可以使用
    datetime

    我们导入它并调用
    datetime.datetime.now()
    方法,该方法为我们提供了一个
    datetime
    对象,其中包含当前日期和时间。 然后,我们可以使用所述对象计算出当前的月份。如下所示:

    导入日期时间
    now=datetime.datetime.now()
    打印(现在。月份)
    
    请注意,这将返回一个整数。要获取月份名称,我们可以使用日历api(也可以在标准库中找到)

    导入日期时间
    导入日历
    now=datetime.datetime.now()
    打印(日历.月份\名称[现在.月份])
    
    现在我们有一个月的时间了,我们可以将数据添加到字典中

    为了跟踪以前输入的数据,我们需要一些持久性存储。最简单的方法是创建一个文件,并以
    json
    格式向其中写入数据

    首先从标准库(再次)导入
    json
    ,然后创建一个我们将写入文件的字典

    import os
    
    # our data file name
    data_file_name = 'data.json'
    
    # check if there is an existing file
    data_present = os.path.isfile(data_file_name)
    
    # if present, open the file in read only mode and fetch it's contents.
    if data_present:
        with open(data_file_name, 'r') as _f:
            json_data = _f.read()
    else:  # data file not found, create one and a dummy json object.
        json_data = json.dumps({calendar.month_name[i]: [] for i in range(1,13)})
        with open(data_file_name, 'w+') as _f:
            _f.write(json_data)
    
    # try parsing json data
    try:
        data = json.loads(json_data)
    except ValueError:
        print('Failed to decode json data')
    

    现在我们有了缺失的部分,我们可以将代码添加到您的应用程序中

    导入日期时间
    导入日历
    导入json
    导入操作系统
    now=datetime.datetime.now()#获取当前日期和时间
    月=日历.月\名称[现在.月]#当前月
    #从json文件读取数据。
    #我们的数据文件名
    data\u file\u name=“data.json”
    #检查是否存在现有文件
    data\u present=os.path.isfile(数据文件名)
    #如果存在,以只读模式打开文件并获取其内容。
    如果数据_存在:
    打开(数据文件名,“r”)为
    json_data=_f.read()
    否则:#未找到数据文件,请创建一个和一个伪json对象。
    json_data=json.dumps({calendar.month_name[i]:[]表示范围(1,13)内的i)
    打开时(数据文件名为“w+”)为:
    _f、 写入(json_数据)
    #尝试解析json数据
    尝试:
    data=json.load(json_数据)
    除值错误外:
    打印(“未能解码json数据”)
    bookcode=str(输入(“输入要返回的图书代码:”))
    day=int(输入(“输入借用天数:”)
    惩罚=无#确保我们有惩罚变量
    
    如果0,您能给出一个用例和您期望的输出吗?