new to Python or any programming languages, started learning a few weeks ago. Basically I need to figure out a code that will take a user input of number of days and translate it into weeks. Sounds easy enough, but say the user inputs 8 days, that would be considered two weeks. Any input must be rounded up to the nearest week.
So far I have
days = int(input("Please enter the number of days you will have the car: "))
weeks = (days/7)
but after that I am not sure how to make the code round the input up to the highest week.
Thanks for any help in advance!
Mezy
You should also use the modulus operator
weeks = (days // 7 ) + (1 if days % 7 != 0 else 0)
Related
I cannot figure out the approach to this as the principle amount shall change after every year(if calculated annually, which shall be the easiest). Eventual goal is to calculate exact number of years, months and days to earn say 150000 as interest on a deposit of 1000000 at an interest rate of say 6.5%. I have tried but cannot seem to figure out how to increment the year/month/day in the loop. I don't mind if this is down voted because I have not posted any code(Well, they are wrong). This is not as simple as it might seem to beginners here.
It is a pure maths question. Compound interest is calculated as follows:
Ptotal = Pinitial*(1+rate/100)time
where Ptotal is the new total. rate is usually given in percentages so divide by 100; time is in years. You are interested in the difference, though, so use
interest = Pinitial*(1+rate/100)time – Pinitial
instead, which is in Python:
def compound_interest(P,rate,time):
interest = P*(1+rate/100)**time - P
return interest
A basic inversion of this to yield time, given P, r, and target instead, is
time = log((target+Pinitial)/Pinitial)/log(1+rate/100)
and this will immediately return the number of years. Converting the fraction to days is simple – an average year has 365.25 days – but for months you'll have to approximate.
At the bottom, the result is fed back into the standard compound interest formula to show it indeed returns the expected yield.
import math
def reverse_compound_interest(P,rate,target):
time = math.log((target+P)/P)/math.log(1+rate/100)
return time
timespan = reverse_compound_interest(2500000, 6.5, 400000)
print ('time in years',timespan)
years = math.floor(timespan)
months = math.floor(12*(timespan - years))
days = math.floor(365.25*(timespan - years - months/12))
print (years,'y',months,'m',days,'d')
print (compound_interest(2500000, 6.5, timespan))
will output
time in years 2.356815854829652
2 y 4 m 8 d
400000.0
Can we do better? Yes. datetime allows arbitrary numbers added to the current date, so assuming you start earning today (now), you can immediately get your date of $$$:
from datetime import datetime,timedelta
# ... original script here ...
timespan *= 31556926 # the number of seconds in a year
print ('time in seconds',timespan)
print (datetime.now() + timedelta(seconds=timespan))
which shows for me (your target date will differ):
time in years 2.356815854829652
time in seconds 74373863.52648607
2022-08-08 17:02:54.819492
You could do something like
def how_long_till_i_am_rich(investment, profit_goal, interest_rate):
profit = 0
days = 0
daily_interest = interest_rate / 100 / 365
while profit < profit_goal:
days += 1
profit += (investment + profit) * daily_interest
years = days // 365
months = days % 365 // 30
days = days - (months * 30) - (years * 365)
return years, months, days
years, months, days = how_long_till_i_am_rich(2500000, 400000, 8)
print(f"It would take {years} years, {months} months, and {days} days")
OUTPUT
It would take 1 years, 10 months, and 13 days
The task I´m given is as follows:
Days of week are numbered as:
0 — Sunday,
1 — Monday,
2 — Tuesday,
...
6 — Saturday.
An integer K in the range 1 to 365 is given. Find the number of the day of the week for the K-th day of the year provided that in this year January 1 was a Thursday.
I´m struggling to create a general and user-friendly code for the given problem. Thanks for any help!
date=int(input('enter the K-th day: '))
if(day==0):
print("Sunday")
if(day==1):
print("Monday")
if(day==2):
print("Tuesday")
if(day==3):
print("Wednesday")
if(day==4):
print("Thursday")
if(day==5):
print("Friday")
if(day==6):
print("Saturday")
We know that day 1 was a Thursday, this means we also know that day 8, 15, 21 and so on are also Thursdays. All these numbers have in common that if you divide them by 7, the remainder is 1.
Day 2 was a Friday, so day 9 and 16 are also Fridays. Divided by 7 the remainder for all Fridays is 2.
If you do this for all weekdays you will notice a pattern ;-) and if you check the python docs you'll stumble across the Modulo operator.
This should point you in the right direction without spoiling the fun. If you need any additional help, just let me know and I'll reify the answer.
here you go!
nday = int(1)
dday=((3+nday)%7)
print(dday)
School Question:
Build a function retirement_age(PMT, i, FV, start_age) that calculates the (whole) age at which your customer can retire, if they:
Invest an amount, PMT at the END of every YEAR (with the first
payment made exactly one year from now),
at an interest rate of i% per year, compounded annually.
They require an amount of AT LEAST FV in order to be able to afford
retirement.
They just turned start_age years old.
I am struggling to solve the number of years PMT would take to reach FV
This is my code:
def retirement_age(PMT, i, FV, start_age):
count = 0
while PMT <= FV: #PMT set to loop till it reaches FV
PMT = PMT * (1+i)
count = count + 1 #adds 1 to count value until while loop satisfied
age = count + start_age #adds count value to start_age to determine retirement age
return int(age) #returns age
print (retirement_age(20000, 0.1, 635339.63, 20))
my answer with this code:
57
The answer is supposed to be:
35
I can't tell what I'm doing wrong. And the task specifically mentions that we are not allowed to import external functions like math for example, which means I can't use math.log() which would probably solve all my problems.
First, I'll note that broad debugging questions like this aren't very appropriate for SO.
Having said that, I played around with it and after reading the specs again, I found the issue(s). I figured I might as well post it.
You only need to keep calculating while the principal is less than the future value. You can stop once they're equal.
The main issues however were that you aren't adding any money each year. You're just accumulating interest on the initial principal. And...
You invested PMT immediately. The investment doesn't happen until the end of the year, as the instructions emphasize. That means at the start of the looping, he has 0 invested. That means he doesn't start accumulating interest until the start of the second loop/year.
def retirement_age(PMT, i, FV, start_age):
age = start_age
p = 0
while p < FV:
p = PMT + p * (1+i)
age += 1
return int(age)
print(retirement_age(20000, 0.1, 635339.63, 20))
# 35
I introduced p to keep track of the running balance since it's separate from what's being added each year. Your logic for keeping track of age was also a little convoluted, so I simplified it down a bit.
I want to make a calculator that calculates someones age in seconds and all the programs that do that only get the days. Can someone help me please?
This sounds more like an algorithm problem. What you would want though is to just keep breaking years into smaller units so for example.
1 year = 365.25 days -> 1 day = 24 hours -> 1 hour = 60 minutes -> 1 minute = 60 seconds
This should be relatively easy to implement in code.
How do I calculate relative years + days between two dates in excel? I want to know the number of years and any additional days (outside of the full years). Dividing the total days by 365 doesn't work because of leap years. For example:
Cell A1 = '1/31/2015'
Cell B1 = '2/1/2025'
Cell C1 = '2/11/2025'
=(B1-A1)/365 # 10.0109 = 10 years + 4 days, looking for 10 years + 0 days
=(C1-A1)/365 # 10.0383 = 10 years + 14 days, looking for 10 years + 10 days
Is there an easy way to calculate this?
Note - this question is not a duplicate of How to find the difference between dates in VBA -- which is a question about calculating the difference between dates. This question is asking how to calculate the difference of both years and days, such that the difference in days is for the final year only, and are not incorrectly including leap year days for previous years.
You can use the documented function DATEDIF
=DATEDIF(startdate,enddate,"Y")
This will give you the difference in whole years
“Y” Returns the period difference as complete years.
“M” Returns the period difference as complete months.
“D” Returns the number of days in the period.
“MD” Returns the difference between the days in ‘Start_Date’ and ‘End_Date’. Here the months and years of the dates are ignored.
“YM” Returns the difference between the months in ‘Start_Date’ and ‘End_Date’. Here the days and years of the dates are ignored
“YD” Returns the difference between the days of ‘Start_Date’ and ‘End_Date’. Here the years of the dates are ignored.
Excel Solution :
=DATEDIF(A1,A2,"y") & " years, " & DATEDIF(A1,A2,"ym") & " months, " & DATEDIF(A1,A2,"md") & " days"
Please refer this link