day of the week issue always prompts monday no matter the entry - dayofweek

The outcome of this is when you enter any number 1-7 you will get the day of that number. for some reason it will always prompt Monday. How can I fix this?
'#Enter a number range 1-7 for the day of the week Example 1=Monday
#variables to represent the days of the week
num = float(input ("Enter the number for the day of week"))
Monday_number = 1
Tuesday_number = 2
Wednesday_number = 3
Thursday_number = 4
Friday_number = 5
Saturday_number = 6
Sunday_number = 7
Other_number = 8
#the day of the week
if Monday_number == 1:
print('Monday')
elif Tuesday_number == 2:
print('Tuesday')
elif Wednesday_number == 3:
print('Wednesday')
elif Thursday_number == 4:
print('Thursday')
elif Friday_number == 5:
print('Friday')
elif Saturday_number == 6:
print('Saturday')
elif Sunday_number == 7:
print('Sunday')
else:
if Other_number > 7:
print('Invalid number entered')'

You're not comparing num, the user input, to anything. In your if statements, you should be sequentially comparing num to each of the day of week constants. Better yet, you could be using a lookup table:
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
user_input = input('Enter the day of the week: ')
print(days[int(user_input)])

Step through your algorithm mentally, line-by-line. What happens when you get to the first if statement?

Related

how to get total number of earnings using Thinkscript?

I want to count the total number of earnings on the chart. if this is 1 year daily chart, I should get 4 earnings back. no error message,but label is not showing on the chart.
def earningCount = if IsNaN(earningCount) then 0 else if hasEarnings() then earningCount + 1 else earningCount;
AddLabel(yes, "There are total " + earningCount + " earnings");
What you have to do is start with the first day and iterate through each previous day asking hasEarnings(). Unfortunately, without any for/while loop functionality in thinkscript, this will be extremely tedious:
def earningCount;
#get latest date
def today = getYYYYMmDd();
#get first date in chart
def firstDay = first(today);
#get number of days to iterate through:
def numOfDays = CountTradingDays(firstDay,today);
#Ask for each day one at a time: if hasEarnings() then earningCount + 1 else Double.NaN;
#today
today
#day before
today[1]
#day before that... etc..
today[2]
#... until first day in chart
today[numOfDays]
Not the optimal solution you would have wanted. Alternatively, you could ask how many years in the chart and multiple by 4 as you know there are usually 4 earnings/yr...
def earningCount = if IsNaN(earningCount[1]) then 0 else if hasEarnings() then earningCount[1] + 1 else earningCount[1];
AddLabel(yes, "There are total " + earningCount + " earnings");

How to get the number of Days in a Specific Month between Two Dates in Python

I have two date fields campaign_start_date and campaign_end_date. I want to count the number of days in each month that comes in-between the campaign_start_date and campaign_end_date.
eg:
campaign_start_date = September 7 2017
campaign_end_date = November 6 2017
The solution should be :
Total No:of days = 61 days
No: of months = 3 months
Month 1 = 9/7/2017 to 9/30/2017
Month 2 = 10/1/2017 to 10/31/2017
Month 3 = 11/1/2017 to 11/6/2017
No:of days in Month 1 = 24 days
No:of days in Month 2 = 31 days
No:of days in Month 3 = 6 days
How can I achieve this using Python?
So far I have achieved:
#api.multi
def print_date(self):
start_date = datetime.strptime(self.start_date, "%Y-%m-%d %H:%M:%S")
end_date = datetime.strptime(self.end_date, "%Y-%m-%d %H:%M:%S")
campaign_start_date = date(start_date.year,start_date.month,start_date.day)
campaign_end_date = date(end_date.year,end_date.month,end_date.day)
duration = (campaign_end_date-campaign_start_date).days
return True
Calculate the duration in days:
from datetime import date
campaign_start_date = date(2017, 9, 7)
campaign_end_date = date(2017, 10, 6)
duration = (campaign_end_date-campaign_start_date).days
print campaign_start_date, campaign_end_date, duration
Some hints for further calculations:
import calendar
campaign_end_month_start = campaign_end_date.replace(day=1)
days_in_month_campaign_end = (campaign_end_date - campaign_end_month_start).days + 1
range_startmonth = calendar.monthrange(campaign_start_date.year, campaign_start_date.month)
campaign_start_month_ends = campaign_start_date.replace(day=range_startmonth[1])
days_in_month_campaign_begins = (campaign_start_month_ends - campaign_start_date).days
This way you can calculate the number of days in each month of the campaign (keep in mind to check if campaign_end_date is in another month than campaign_start_date
For calculations you can also access the fields of a date, e.g.
campaign_start_date.day
campaign_start_date.month
campaign_start_date.year
To calculate the number of involved month in your campaign and to get a list of the month to calculate the duration per month you can use this (based on the answer of m.antkowicz in Python: get all months in range?). It's important to set the day to 1 (current = current.replace(day=1)) before and inside the loop, otherwise you skip a month when your startdate is 31st of a month and the next month is shorter than 31 days or if you have a longer period:
from datetime import date, datetime, timedelta
current = campaign_start_date
result = [current]
current = current.replace(day=1)
while current <= campaign_end_date:
current += timedelta(days=32)
current = current.replace(day=1)
result.append(datetime(current.year, current.month, 1))
print result, len(result)
which prints (when you use current.strftime('%Y-%m-%d'):
['2017-09-07', '2017-10-01', '2017-11-01'] 3
now you can loop over the result list and calculate the number of days per months:
durations= []
for curr in result:
curr_range = calendar.monthrange(curr.year, curr.month)
curr_duration = (curr_range[1] - curr.day)+1
if (curr.month < campaign_end_date.month):
durations.append(curr_duration)
else:
durations.append(campaign_end_date.day)
print durations
which gives you the desired "No:of days in Month x" as a list:
[24, 31, 6]
This is the robust solution which takes care of dates from different years.
def get_months_and_durations(start_date,end_date):
current = start_date
result = [current]
current = current.replace(day=1)
while current <= end_date:
current += timedelta(days=32)
current = current.replace(day=1)
result.append(datetime(current.year, current.month, 1).date())
durations= []
for curr in result[:-1]:
curr_range = calendar.monthrange(curr.year, curr.month)
curr_duration = (curr_range[1] - curr.day)+1
if ((curr.month == end_date.month) & (curr.year == end_date.year)):
durations.append(end_date.day)
else:
durations.append(curr_duration)
return result[:-1],durations

Filter the data for particular 15 days of all years in django

I am trying to print the data for particular 15 days of every year.
For example to get the Employee's details who has birthdays with in 15 days.
today = datetime.now()
start_day = today.day
start_month = today.month
end_day = today + timedelta(days=15)
end_date = end_day.day
end_month = end_day.month
user_dob_obj = UserProfile.objects.filter(Q(date_of_birth__month__gte=start_month, date_of_birth__day__gte=start_day) &
Q(date_of_birth__month__lte=end_month, date_of_birth__day__lte=end_date))
Update
Sorry I misunderstood your question. You can use if statement to check if the month is the same 15 days later. Then use the or logical operation to make sure birthdays in current and next month are filtered.
today = datetime.now()
end_date = today + timedelta(days=15)
if today.month == end_date.month:
user_dob_obj = user_dob_obj.filter(date_of_birth__month=today.month, date_of_birth__day__gte=today.day, date_of_birth__day__lte=end_date.day)
else:
user_dob_obj = queryset.filter(Q(date_of_birth__month=today.month, date_of_birth__day__gte=today.day) | Q(date_of_birth__month=end_date.month, date_of_birth__day__lte=end_date.day))

Adding 6 business days

I have a function that will calculate 6 days, it works, and it's wonderful and all, but I need a way to skip Saturday and Sunday. How can I fix this function in order to have it skip Saturday and Sunday?
def calc_bus_day(start_day):
if start_day.isoweekday() in range(1, 5):
shift = 6
returnDate = start_day + datetime.timedelta(days=shift)
if returnDate.isoweekday() == 0:
return "{:%m-%d-Y}".format(returnDate + datetime.timedelta(days=1))
elif returnDate.isoweekday() == 5:
return "{:%m-%d-%Y}".format(returnDate + datetime.timedelta(days=2))
else:
return "{:%m-%d-%Y}".format(returnDate)
As shifting for 6 days always includes shifting over a weekend, you can shift for 8 days (6 days + Saturday and Sunday):
def calc_bus_day(start_day):
if start_day.isoweekday() in range(1, 5):
shift = 8
< your code >
You can use this code that I use. The attempt it is add not just 6 days, but any number of days.
from datetime import datetime, timedelta, date
def getNextBusinessDays(date, num):
for i in range(0, num):
date = getNextBusinessDay(date)
return date
def getNextBusinessDay(fromDate):
nextBuinessDate = datetime.strptime(fromDate, "%Y-%m-%d")
nextBuinessDate = nextBuinessDate + timedelta(days=1)
if date.weekday(nextBuinessDate) not in range(0, 5):
nextBuinessDate = nextBuinessDate + timedelta(days=1)
if date.weekday(nextBuinessDate) not in range(0, 5):
nextBuinessDate = nextBuinessDate + timedelta(days=1)
return nextBuinessDate.strftime('%Y-%m-%d')
For example getNextBusinessDays('2016-10-20', 6) will produce "2016-10-28"

Python Salary Program Using Conditionals

I get an error # line 19, the Bonus function and I can't figure out why. I'll probably get an error for the other functions too. I've checked my spaces, my numbers vs. my strings, and my DOM. My first problem were about my globals and I fixed it from global comrate to `comrate = 0; . I've got debugging blindness. Thank you guys in advance!
def main():
#Welcome user and get sales number
print("Welcome to the Bonus Qualification Calculator! Please honestly answer the following questions:")
name = str(input("What is your name? "))
sales = float(input("What is your sales total? "))
jobtime = float(input("How many months have you been with the company? "))
vacationtime = float(input("How many vacation days have you taken? "))
#Define Global Vars
comrate = 0;
compedsalary = 0;
bonussalary = 0;
finalsalary = 0;
#Begin calculations
Bonus(sales, jobtime)
vacation(vacationtime)
print(str(name) + ", your salary based on the information you provided is " + str(format(finalsalary,'.2f'))
def Bonus(sales,jobtime):
#Calcultate commission
if sales < 10000:
comrate = 0
elif sales > 10000 and sales <= 1000000:
comrate = .02
elif sales >= 100001 and sales <= 500000:
comrate = .15
compedsalary = float(comrate * 2000)
if jobtime > 3:
bonussalary = float(compedsalary + 1000)
else:
print("You don't qualify for a bonus due to your limited time at the company.")
elif sales >= 500001 and sales <= 1000000:
comrate = .28
compedsalary = float(comrate * 2000)
if jobtime > 3:
bonussalary = float(compedsalary + 5000)
else:
print("You don't qualify for a bonus due to your limited time at the company.")
elif sales > 1000000:
comrate = .35
compedsalary = float(comrate * 2000)
if jobtime > 3:
bonussalary = float(compedsalary + 100000)
elif jobtime > 60:
bonussalary = float(compedsalary + 101000)
else:
print("You don't qualify for a bonus due to your limited time at the company.")
def vacation(finalsalary):
if vacation > 3:
finalsalary = float(bonussalary - 200)
else:
finalsalary = bonussalary
main()
You're using full quotes where you should be using apostrophes. You're using contractions in your print statements, which confuses Python. Just put "do not" instead of "don't" in your print statements.