Leonardo
Leonardo

Reputation: 170

How to store data with Time Module in python

I want to write a very simple program which calculates how much you earned with work during the month (it's just for practicing). Idea is next: every time you run the program it prints out today's date and asks user to enter the amount of hour he worked. Then it calculates today's and whole month earnings and prints it out. My problem is that I don't know how to "manipulate" time module.

write now I wrote this: (which has today's and monthly earnings the same)

import time

Today = time.strftime('%d/%m/%Y')

iph = 8.51 # Income Per Hour
print 'Today Is: ' , Today # Print Current Date
TodayHours = input('How many hours did you work today? ')
MonthlyEarning = TodayHours * iph # Amount of Money Earned During the Whole Month
DayE = TodayHours * iph # Today's Earning
print 'Today You Earned: ' , DayE , 'Euros'
print 'Total Monthly Earnings: ' , MonthlyEarning , 'Euros'

What I want to do is that, if I run it today and write that I worked 5 hours it stores it as today's date and when I run it again tomorrow and type another 5 hours it will print today's earning as 5*8.51 and as Monthly earning (5+5) * 8.51

How can I do that?

Upvotes: 0

Views: 74

Answers (1)

dcc310
dcc310

Reputation: 1076

As plamut said, you need some storage. Here is a program to get you started that will give interaction like so:

Enter 'print' or some hours, format like 12.34, 0.56,etc
1.00
Enter 'print' or some hours, format like 12.34, 0.56,etc
2.01
Enter 'print' or some hours, format like 12.34, 0.56,etc
print
You have earned 24.08 today and 24.08 this month
Enter 'print' or some hours, format like 12.34, 0.56,etc

If you know how to read and write to files, and print the current date, take substrings, and split strings, you should be able to read through this.

import datetime

file_name = "work_log.txt"
hourly_wage = 8.00

def get_year_month_day_string():
    return datetime.datetime.now().strftime("%Y-%m-%d")

def add_to_log(num_hours):
    earnings = num_hours * hourly_wage
    # open in append mode
    with open(file_name, "a") as f_out:
        f_out.write("%s,%s\n" % (get_year_month_day_string(), earnings))

def read_earnings():
    year_month_day_prefix = get_year_month_day_string()
    year_month_prefix = year_month_day_prefix[0:7]
    # open in read mode
    with open(file_name, "r") as f_in:
        day_sum = 0
        month_sum = 0
        lines = f_in.readlines()
        for line in lines:
            date = line.split(",")[0]
            amount = float(line.split(",")[1])
            if date.startswith(year_month_day_prefix):
                day_sum += amount
            if date.startswith(year_month_prefix):
                month_sum += amount
        print "You have earned %s today and %s this month" % (day_sum, month_sum)

if __name__ == "__main__":
    while True:
        user_input = raw_input("Enter 'print' or some hours, format like 12.34, 0.56,etc\n")
        if user_input == "print":
            read_earnings()
            next
        else:
            entered_hours = float(user_input)
            add_to_log(entered_hours)

You could of course edit the file if you ever made a mistake or a duplicate entry. The file work_log.txt just looks like:

2016-06-29,8.0
2016-06-29,16.08

Upvotes: 1

Related Questions