Python, printing things a select number of times - python

I'm new to coding and am having an issue with my program. I have to get sales information from a file and print it in a certain format. This is the code:
#Looping program to read file per line
for line in lines:
# Formatting line to read later
line = line.strip().split(',')
year = line[0]
year = int(year)
month = line[1]
month = int(month)
day = line[2]
day = int(day)
linePoint = date(year, month, day)
cost = line[3]
cost = float(cost)
#Finding the lines within the start and end date
if (linePoint >= startDate) and (linePoint <= endDate):
printcost = (cost / 100)
printcost = int(printcost)
print "%s.%s.%s" % (startYear, startMonth, startDay)
print "0%s:" % printNum, "*" * printcost
newcost = newcost + cost
printNum += 1
When I use the %s.%s.%s it's printing the date above the sales, I want it to print that above the other print statement once per month, and be able to increase it once the month is up.
Also in the print "0%s:" % printNum, "*" * printcost statement I would like it to only print the zero for the first nine days.
Essentially my question is how in Python do I run something a certain number of times, but the number of times is dependent on the user and correlate with the date, and in order to do that the computer needs to be able to recognize the date. Sorry for the vagueness.

If you want the output to be '01', '02', ..., '10', '11', ... then the format you want to use is:
print "%02d" % printNum
As for printing the header out at the start of each new month (that's how I'm reading the first part of your question, you could do something like:
old_month = 0
for line in lines:
# do stuff
month = whatever...
if month != old_month:
# print header here
old_month = month
#rest of loop

I'm almost sure this is what you want. Note the "%02d" format specifier, which gives you the leading zero, and the check to see if the month has changed via if month != current_month.
current_month, print_num, new_cost = None, 0, 0
for line in lines:
fields = line.strip().split(',')
year = int(fields[0])
month = int(fields[1])
day = int(fields[2])
cost = float(fields[3])
line_date = date(year, month, day)
#Finding the lines within the start and end date
if startDate <= line_date <= endDate:
if month != current_month:
print "%s.%s.%s" % (year, month, day)
current_month = month
print_cost = int(cost / 100)
print "%02d: %s" % (print_num, "*" * print_cost)
new_cost += cost
print_num += 1

Related

How to make a loop that validates the user-input dates based on the functions created?

I am not using any datetime module. I created my own functions to calculate the day, month, and year. I want to calculate the refunds based on the date. If the date is invalid, it should ask the user to try again until a date is true.
year = 0
month = 0
day = 0
money_owed = 0
def if_leap_year(year):
if (year % 400 == 0): return 366
elif (year % 100 == 0): return 365
elif (year % 4 == 0): return 366
else:
return 365
#print(if_leap_year(year))
def days_in_month(month, year):
if month in {1, 3, 5, 7, 8, 10, 12}:
return 31
if month == 2:
if if_leap_year(year):
return 29
return 28
return 30
#print(days_in_month(month, year))
def is_valid_date(year, month, day):
if days_in_month(month, year)<day:#checks if the given day is possible
given the month
return False
else:
return True
def days_left_in_year(month, day, year):
daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31]
daysLeft = (if_leap_year(year) if month < 3 else 365) -
sum(daysInMonth[:month - 1]) - day
return daysLeft
def refund_period():
month = int(input("Enter the month of the year: "))
day = int(input("Enter the day of the year: "))
year = int(input("Enter the year to determine the number of days: "))
if is_valid_date(year , month , day):
money_owed = (days_left_in_year(month, day, year) /
if_leap_year(year)) * 278
return round(money_owed, 2)
else:
print("Your date is invalid, try again.")
while is_valid_date(year, month, day):
print('you will be refunded','$', + refund_period())
break
else:
print("Your date is invalid, try again.")
I am getting:
you will be refunded $ -8.38
even though the calculation shouldn't be performed since the date is invalid
You are setting year =0 , month =0, day = 0 in first loop.
Also the while is not clear. All your functions return an int so never validate if the date is correct.
Maybe you can create a function to validate the date something like this :
def is_valid_date(year , month , day):
if month <1 or month >12: #Validate a allowed month
return False
if day <1 or day > days_in_month(month, year): # validate an allowed day for the month
return False
return True
and you can change this function :
def refund_period():
month = int(input("Enter the month of the year: "))
day = int(input("Enter the day of the year: "))
year = int(input("Enter the year to determine the number of days: "))
if is_valid_date(year , month , day):
money_owed = (days_left_in_year(month, day, year) / if_leap_year(year)) * 278
return round(money_owed, 2)
else :
print("Your date is invalid, try again.")
Just a couple of comments:
You are getting the year, month, and day using input() so you don't need to create global variables for that.
you don't need to ask if if_leap_year(year) == 365 or 366 because this function returns 365 or 366 so you can use it directly when you calculate the money_owned, as I do.
Also you can use if_leap_year(year) instead
(if_leap_year(year) if month < 3 else 365) . That functions return 366 or 365, you dont need to validate again.
And you can use list comprehension for you daysInMonth variable inside days_left_in_year function :
daysInMonth = [days_in_month(m, year) for m in range(1,13)]
Your while loop is not comparing the function value but just checking if the object exists. Instead of conditions like while days_left_in_year(month, day, year), use conditions like while days_left_in_year(month, day, year)<30 (assuming you wanted to deny refunds on orders older than 30 days.
To validate dates, add the following function under your comment #print(days_in_month(month, year)):
def is_valid_date(year, month, day)
if days_in_month(month, year)<day:#checks if the given day is possible given the month
return False
else:
return True
then your condition should look something like this:
if ((is_valid_date(year, month, day) == True) and (month<13)):
print('you will be refunded','$', + refund_period())
else:
print("Your date is invalid, try again.")

Python3 - Updating a 'power' variable in an inner loop modifies the outer loop's aggreggation variable

I read through all the files in a directory (csv files) - I ignore the first 7 'header ' lines of each, then find the time and power values from line 8 to line 151. Each of the lines I read is of the form [example lines 6-9 & 46-48]
Time;Power
HH:mm;kW
00:10;0.000
00:20;0.000
<snip>
06:20;0.012
06:30;0.042
06:40;0.060
My code successfully ignores all the post-midnight readings of zero and correctly identifies the first non-zero power reading at 06:20. It uses the time (hours and minutes) to a adjust a variable that contains the timestamp for the day and stores the now hour/minute inclusive timestamp and the power in a complex variable timePower[(timestamp),(power)]
It then appends the variable timePower[(),()] to the variable dayPower[] and starts again for the next line of the file.
For a reason I cannot work out, the next line parses fine until the point at which I update timePower[0] with the new timestamp and timePower[1] with the new power - updating these variables seems to also update the existing entry in the dayPower[] variable (dayPower[0]), now, right before I'm about to append a new entry, the old entry looks the same as the new one. The new line successfully appends. This errant behaviour repeats until I've finished reading all the non-zero values and all lines in dayPower[] look the same as the final entry. The relevant function is listed below:
def parse_power_values(path, filename, theDate):
timePower = [(),()] #a list that will be assigned two items; the timestamp and the power for that timeslot
dayPower = [] # A list that will have all the timePowers for the day appended
currentFile = open(path + filename,'r')
for i, line in enumerate(currentFile):
if i <= 7:
doingSomething = True
#print 'header' + str(i) + '/ ' + line.rstrip()
elif ((i > 7) and (i <= 151)):
lineParts = line.split(';')
theTime = lineParts[0].split(':')
theHour = theTime[0]
theMin = theTime[1]
timestamp = theDate.replace(hour=int(theHour),minute=int(theMin)) #theDate is a timestamp obj with the current date but hour & minute at 00:00 to start with.
power = lineParts[1].rstrip()
if power == '-.---':
power = 0.000
if (float(power) > 0):
#append_to_database(con, timestamp,power)
timePower[0] = timestamp
timePower[1] = power
dayPower.append(timePower)
elif i > 151:
return dayPower
#break
currentFile.close()
#jasonharper was right and my modified function now looks like this:
def parse_power_values(path, filename, theDate):
# timePower = [(),()] #a list that will be assigned two items; the timestamp and the power for that timeslot
dayPower = [] # A list that will have all the timePowers for the day appended
currentFile = open(path + filename,'r')
for i, line in enumerate(currentFile):
if i <= 7:
doingSomething = True
#print 'header' + str(i) + '/ ' + line.rstrip()
elif ((i > 7) and (i <= 151)):
lineParts = line.split(';')
theTime = lineParts[0].split(':')
theHour = theTime[0]
theMin = theTime[1]
timestamp = theDate.replace(hour=int(theHour),minute=int(theMin))
power = lineParts[1].rstrip()
if power == '-.---':
power = 0.000
if (float(power) > 0):
dayPower.append([timestamp,power])
elif i > 151:
return dayPower
#break
currentFile.close()

Select dates in index

i have start date and end date and dataframe with daily observations. The problem is that i can't find a way, which will enable me select dates with periodicity of 3 months
for example:
2003-01-03 + 3 months = 2003-04-03 and so on
output should consist of 20 rows because 5 years with 3 months periodicity, including start and end dates
EDIT: Old solution didn't work for all cases. Therefore a new one:
start, end = returns.index[0], returns.index[-1]
length = (end.year - start.year) * 12 + (end.month - start.month)
if length % 3 == 0 and end.day >= start.day:
length += 3
new_index = []
for m in range(3, length, 3):
ydelta, month = divmod(start.month + m, 12)
day = pd.Timestamp(year=start.year + ydelta, month=month, day=1)
day += pd.Timedelta(f'{min(start.day, day.days_in_month) - 1}d')
new_index.append(day)
new_index = pd.DatetimeIndex(new_index)
returns = returns.loc[new_index]
Another version which has some slight inaccuracies around the month ends but is more compact:
add_3_months = pd.tseries.offsets.DateOffset(months=3)
new_index = pd.date_range(returns.index[0] + add_3_months,
returns.index[-1],
freq=add_3_months)
returns = returns.loc[new_index]

How to solve the weekday function problem Python

I am very new to Python and we were told to write the weekday function without any modules like e.g. daytime etc.
But it doesn't work and i am not sure where is a problem
def weekDay (day,month,year):
global y0
global m0
global x
y0 = 0
m0 = 0
day,month,year = int(input("Enter a day: "))
month = int(input("Enter a month: "))
year = int(input("Enter a year:"))
a = (day + x + (31 * m0) // 12) % 7
for m0 in a:
m0 = month + 12 * ((14 - month) // 12) - 2
for x in a:
x = y0 + y0 // 4 - y0 // 100 + y0 // 400
for y0 in x:
y0 = year - ((14 - month) // 12)
if a == 0:
print("Sunday")
elif a == 1:
print("Monday")
elif a == 2:
print("Tuesday")
elif a == 3:
print("Wednesday")
elif a == 4:
print("Thursday")
elif a == 5:
print("Friday")
else:
print("Error")
return weekDay(a)
'''
here is the formula we were given:
[![formula][1]][1]
[1]: https://i.stack.imgur.com/iBv30.png
This should help:
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime('%A')
'Friday'
>>>
Global variables not defined anywhere and I am not able to understand the logic you are trying to write. So written a function based on a aptitude trick.
def weekday(day,month,year):
"""
This function is written based upon aptitude trick
to obtain day from given a date.
Input date example : 15-5-2020
Link for logic : https://www.youtube.com/watch?v=rJ0_GWDTdD4
"""
# for different months we are assigning specific number to that month
months = {1:1, 2:4, 3:4, 4:0, 5:2, 6:5, 7:0, 8:3, 9:6, 10:1, 11:4, 12:6}
# assigning days to a number
day_name = {1:'Sunday', 2:'Monday', 3:'Tuesday', 4:'Wednesday', 5:'Thursday',
6:'Friday', 0:'Saturday'}
# to get the year in between 1600 and 2000. since we are assiging values
# for the years also
while year not in range(1600,2000):
if year>2000:
year-=400
if year<1600:
year+=400
# assigning values to years
if year in range(1600,1700):
yr = 6
if year in range(1700,1800):
yr = 4
if year in range(1800,1900):
yr = 2
if year in range(1900,2000):
yr = 0
# assigning last two numbers of year to last
first = year//100
last = year - (first * 100)
# obtaining remainder
res = (day + months[month] + yr + last + (last//4))%7
#returning the day_name
return day_name[res]
day,month,year = list(map(int,input("Enter date in format dd-mm-yyyy : ").split('-')))
print(weekday(day,month,year))
Hope, you are satisfied with logic.

Making a time adding function in python

I'm trying to build a function that recieves a date and adds days, updating everything in case it changes, so far i've come up with this:
def addnewDate(date, numberOfDays):
date = date.split(":")
day = int(date[0])
month = int(date[1])
year = int(date[2])
new_days = 0
l = 0
l1 = 28
l2 = 30
l3 = 31
#l's are the accordingly days of the month
while numberOfDays > l:
numberOfDays = numberOfDays - l
if month != 12:
month += 1
else:
month = 1
year += 1
if month in [1, 3, 5, 7, 8, 10, 12]:
l = l3
elif month in [4, 6, 9, 11]:
l = l2
else:
l = l1
return str(day) + ':' + str(month) + ':' + str(year) #i'll deal
#with fact that it doesn't put the 0's in the < 10 digits later
Desired output:
addnewDate('29:12:2016', 5):
'03:01:2017'
I think the problem is with either the variables, or the position i'm using them in, kinda lost though..
Thanks in advance!
p.s I can't use python build in functions :)
Since you cannot use standard library, here's my attempt. I hope I did not forget anything.
define a table for month lengths
tweak it if leap year detected (every 4 year, but special cases)
work on zero-indexed days & months, much easier
add the number of days. If lesser that current month number of days, end, else, substract current month number of days and retry (while loop)
when last month reached, increase year
add 1 to day and month in the end
code:
def addnewDate(date, numberOfDays):
month_days = [31,28,31,30,31,30,31,31,30,31,30,31]
date = date.split(":")
day = int(date[0])-1
month = int(date[1])-1
year = int(date[2])
if year%4==0 and year%400!=0:
month_days[1]+=1
new_days = 0
#l's are the accordingly days of the month
day += numberOfDays
nb_days_month = month_days[month]
done = False # since you don't want to use break, let's create a flag
while not done:
nb_days_month = month_days[month]
if day < nb_days_month:
done = True
else:
day -= nb_days_month
month += 1
if month==12:
year += 1
month = 0
return "{:02}:{:02}:{:04}".format(day+1,month+1,year)
test (may be not exhaustive):
for i in ("28:02:2000","28:02:2004","28:02:2005","31:12:2012","03:02:2015"):
print(addnewDate(i,2))
print(addnewDate(i,31))
result:
02:03:2000
31:03:2000
01:03:2004
30:03:2004
02:03:2005
31:03:2005
02:01:2013
31:01:2013
05:02:2015
06:03:2015
of course, this is just for fun. Else use time or datetime modules!

Categories

Resources