I'm converting from all units of time to seconds and for some reason with the smaller units of time (picoseconds and femtoseconds), i'm getting (femtoseconds): 0.00000000000000100000000000000007770539987666107923830718560119501514549256171449087560176849365234375 instead of 0.000000000000001. Does anyone know why?
days = int(input("Enter the amount of days: ")) * 24 * 60 * 60
hours = int(input("Enter the amount of hours: ")) * 60 * 60
minutes = int(input("Enter the amount of minutes: ")) * 60
ms = int(input("Enter the amount of milliseconds: ")) * (10 ** -3)
mcs = int(input("Enter the amount of microseconds: ")) * (10 ** -6)
ns = int(input("Enter the amount of nanoseconds: ")) * (10 ** -9)
ps = int(input("Enter the amount of picoseconds: ")) * (10 ** -12)
fs = int(input("Enter the amount of femtoseconds: ")) * (10 ** -15)
s = days + hours + minutes + ms + mcs + ns + ps + fs
print("The amount of seconds is:", "{0:.50}".format(s))
Floating point numbers cannot be accurately represented in any programming language, simply because there is an infinite number of them. However, what might help you is Decimal: Clarification on the Decimal type in Python
Documentation: https://docs.python.org/3.8/library/decimal.html
Related
I am making a converting program in Python, and in this case it’s from feet to yards. I have having trouble with a specific line of code that is not showing an error message, yet refuses to actually work.
When I run the program and put the following numbers in the input (3, 2, 4, 1) the Yards should be 8, and the feet should be 0, but it stays on 7 yards, and doesn’t add the 3 extra feet into the yard amount.
firstYard = int(input("Enter the Yards: "))
firstFeet = int(input("Enter the Feet: "))
secondYard = int(input("Enter the Yards: "))
secondFeet = int(input("Enter the Feet: "))
print("Yards: ")
print(int(firstYard + secondYard))
print(" Feet: ")
print(int(firstFeet + secondFeet) % 3)
if ((firstFeet + secondFeet) % 3) > 2:
** firstYard += 1
**
The last line is what I’m having trouble with.
Yards = int(input("Enter the Yards: "))
Feet = int(input("Enter the Feet: "))
Yards_2 = int(input("Enter the Yards: "))
Feet_2 = int(input("Enter the Feet: "))
print("\nYards:",(Yards * 3 + Yards_2 * 3 + Feet + Feet_2) // 3, "Feet:",(Yards * 3 + Yards_2 * 3 + Feet + Feet_2) % 3)
You have to scroll to the right more.
The feet should not exceed 2 because 3 feet would be a yard.
This line is causing your problem:
if ((firstFeet + secondFeet) % 3) > 2:
The remainder of division by three cannot be greater than 2, maybe you mean to see if three fits more than twice? Try division instead of modulo if that's the case.
Your test
if ((firstFeet + secondFeet) % 3) > 2:
will always be false, because you're taking the modulo of that sum and any number module 3 cannot be greater than 2.
What you need to add to your yard total is the integer division of the feet total:
firstYard += (firstFeet + secondFeet) // 3
I would structure your code differently:
totalYard = firstYard + secondYard + (firstFeet + secondFeet) // 3
totalFeet = (firstFeel + secondFeet) % 3
*****UPDATE: Thanks to everyone who contributed. The code is alright. I figured there was a problem with the website I was using as IDLE.
I have to write a program in Python that calculates then prints specific information about a loan. The program (code below) consists of 2 functions and I have written them correctly. The only problem I have is that I have to write the second function within the first one. When I run the code, I get this error: NameError: name 'vaam' is not defined
I've just started coding last week, I hope you guys help me out with this.
Here's my program:
# Your function for calculating payment goes here
def loan(principal,annual_interest_rate,duration):
r=(annual_interest_rate)/1200
n=duration*12
if annual_interest_rate==0:
monthly_payment=principal/n
else:
monthly_payment=(principal*(r*(1+r)**n))/((1+r)**n-1)
return monthly_payment
# Your function for calculating remaining balance goes here
def vaam(principal, annual_interest_rate, duration , number_of_payments):
n=duration*12
r=(annual_interest_rate)/1200
if annual_interest_rate==0:
remaining_loan_balance=principal-principal*(number_of_payments/n)
else:
remaining_loan_balance=(principal*((1+r)**n-(1+r)**number_of_payments))/((1+r)**n-1)
return remaining_loan_balance
# Your main program goes here
principal=float(input("Enter loan amount: "))
annual_interest_rate=float(input("Enter annual interest rate (percent): "))
duration=int(input("Enter loan duration in years: "))
print('LOAN AMOUNT:',int(principal),'INTEREST RATE (PERCENT):',int(annual_interest_rate))
print('DURATION (YEARS):',int(duration),'MONTHLY PAYMENT:',int(loan(principal,annual_interest_rate,duration)))
for i in range(1,duration+1):
print('YEAR:',i,'BALANCE:',int(vaam(principal, annual_interest_rate, duration , i*12)),'TOTAL PAYMENT:',int(loan(principal,annual_interest_rate,duration)*12*i))
Try this:
# Your function for calculating payment goes here
def loan(principal, annual_interest_rate, duration):
r = (annual_interest_rate) / 1200
n = duration * 12
if annual_interest_rate == 0:
monthly_payment = principal / n
else:
monthly_payment = (principal * (r * (1 + r) ** n)) / ((1 + r) ** n - 1)
return monthly_payment
# Your function for calculating remaining balance goes here
def vaam(principal, annual_interest_rate, duration, number_of_payments):
n = duration * 12
r = (annual_interest_rate) / 1200
if annual_interest_rate == 0:
remaining_loan_balance = principal - principal * (number_of_payments / n)
else:
remaining_loan_balance = (principal * ((1 + r) ** n - (1 + r) ** number_of_payments)) / ((1 + r) ** n - 1)
return remaining_loan_balance
# Your main program goes here
principal = float(input("Enter loan amount: "))
annual_interest_rate = float(input("Enter annual interest rate (percent): "))
duration = int(input("Enter loan duration in years: "))
print('LOAN AMOUNT:', int(principal), 'INTEREST RATE (PERCENT):', int(annual_interest_rate))
print('DURATION (YEARS):', int(duration), 'MONTHLY PAYMENT:', int(loan(principal, annual_interest_rate, duration)))
for i in range(1, duration + 1):
print('YEAR:', i, 'BALANCE:', int(vaam(principal, annual_interest_rate, duration, i * 12)), 'TOTAL PAYMENT:',
int(loan(principal, annual_interest_rate, duration) * 12 * i))
Output
Enter loan amount: 5000
Enter annual interest rate (percent): 8
Enter loan duration in years: 5
LOAN AMOUNT: 5000 INTEREST RATE (PERCENT): 8
DURATION (YEARS): 5 MONTHLY PAYMENT: 101
YEAR: 1 BALANCE: 4152 TOTAL PAYMENT: 1216
YEAR: 2 BALANCE: 3235 TOTAL PAYMENT: 2433
YEAR: 3 BALANCE: 2241 TOTAL PAYMENT: 3649
YEAR: 4 BALANCE: 1165 TOTAL PAYMENT: 4866
YEAR: 5 BALANCE: 0 TOTAL PAYMENT: 6082
The issue is with your indentation, you put vaam function under loan function.
Keep getting bad input errors on this python code. Can someone walk me through what I'm doing wrong? Thanks. The task is that the code works out time-and-a-half for the hourly rate for all hours worked above 40 hours. Using 45 hours and a rate of 10.50 per hour to test the program, the pay should then be 498.75. I keep getting 708.75...
hrs = input("Enter Hours:")
h = float(hrs)
rate = input("Enter Rate:")
r = float(rate)
double_r = r * 1.5
total = 0.0
if h <= 40.00:
total = h * r
elif h > 40.00:
total = h * double_r
print(total)
hrs = float(input("Enter Hours: "))
rate = float(input("Enter Rate: "))
double_rate = rate * 1.5
total = 0.0
if hrs <= 40.00:
total = hrs * rate
elif hrs > 40.00:
total = ((hrs - 40 ) * double_rate) + (40 * rate)
print(total)
Your problem isn't a coding problem but a math problem: You're multiplying every hour with the double_r rate (45 * 10.5 * 1.5 = 708.75). If you only want the hours above 40 hours to be multiplied with the higher rate then you have to multiply them extra (40 * r for the normal rate and (h-40) * double_r for the rest with the better rate. Your code should look like this:
if h <= 40.00:
total = h * r
elif h > 40.00:
total = 40 * r + (h - 40) * double_r
Is this bad input error or logical error ?
I don't have solution for first one, but I surely have it for the second part.
According to your code, If hours are <=40 , you are multiplying the hour with the rate.
but if it greater than 40, you are multiply the hour with the rate with 1.5 .
Here the logic is going wrong.
You just need to add the extra 1.5 for those hours which are greater than 40.
For that, you would have to modify your total statement.
Something like this :
total = ((h - 40 ) * double_r) + (40 * r)
So for 45 hours with 10.5 rate ,
it would be 40 * 10.5 = 420
and 510.51.5 = 78.75
thus resulting in 498.75
If this helps, please upvote. :)
I am a beginner programmer and have a question regarding the calculation of future investment values based on the following formula :
futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate)numberOfMonths
... ofc the numberOfMonths value is an exponent.
I have created this so far but seem to receive incorrect answers when running the program
#Question 2
investmentAmount = float(input("Enter investment amount: "))
annualInterestRate = float(input("Enter annual interest rate: "))
monthlyInterestRate = ((annualInterestRate)/10)/12
numberOfYears = eval(input("Enter number of years: "))
numberOfMonths = numberOfYears * 12
futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate) **\
numberOfMonths
print("Accumulated value is", futureInvestmentValue)
what do I need to fix in order to get this thing to work, any help would be appreciated thank you
annualInterestRate should be divided by 12 to get monthlyInterestRate.
The correct final formula should be
futureInvestmentValue = investmentAmount * (1 + (monthlyInterestRate/100) ** \
numberOfMonths
You can do it like this:
from __future__ import division # assuming python 2.7.xxxxx
investmentAmount = float(input("Enter investment amount: "))
annualInterestRate = float(input("Enter annual interest rate: "))
monthlyInterestRate = ((annualInterestRate)/10)/12
try:
numberOfYears = int(input("Enter number of years: "))
except Exception, R:
print "Year must be a number"
numberOfMonths = numberOfYears * 12
futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate) **\
numberOfMonths
print("Accumulated value is", futureInvestmentValue)
The errors are in:
monthlyInterestRate = ((annualInterestRate)/10)/12
futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate) **\
numberOfMonths
There are two errors i think. The first is that the interest rate is being divided by 10 when it should be divided by 100. Right now, if you enter 2, its treated as 20% interest because 2/10 = .2.
The second error is that monthlyInterestRate assumes a flat interest rate whereas futureInvestmentValue assumes a compound interest rate. It should be
monthlyInterestRate = (1 + (annualInterestRate/100))**(.1/1.2).
For example(Using /12):
print 0.05/12
print (1+0.05/12)**12
Output:
0.00416666666667
1.05116189788
The monthly interest rate compounded is not equal to the annual interest rate for a single year. Its because in one case you divide by 12, the next case you raise to the raise to the power of 12 which are not equivalent.
Example (Using **1/12)
from __future__ import division
print (1.05**(1/12))**12 #returns 1.05
investmentAmount = eval(input("Enter investment amount: "))
annualInterestRate = eval(input("Enter annual interest rate: "))
monthlyInterestRate = (annualInterestRate) / 10 / 12
numberOfYears = eval(input("Enter number of years: "))
numberOfMonths = numberOfYears * 12
futureInvestmentValue = investmentAmount * (1 + monthlyInterestRate) **\
numberOfMonths
print("Accumulated value is", futureInvestmentValue)
You just have one mistake which is "eval"
I am learning Python and am stuck. I am trying to find the loan payment amount. I currently have:
def myMonthlyPayment(Principal, annual_r, n):
years = n
r = ( annual_r / 100 ) / 12
MonthlyPayment = (Principal * (r * ( 1 + r ) ** years / (( 1 + r ) ** (years - 1))))
return MonthlyPayment
n=(input('Please enter number of years of loan'))
annual_r=(input('Please enter the interest rate'))
Principal=(input('Please enter the amount of loan'))
However, when I run, I am off by small amount. If anyone can point to my error, it would be great. I am using Python 3.4.
Payment calculation-wise, you don't appear to have translated the formula correctly. Besides that, since the built-in input() function returns strings, you'll need to convert whatever it returns to the proper type before passing the values on to the function which expects them to numeric values.
def myMonthlyPayment(Principal, annual_r, years):
n = years * 12 # number of monthly payments
r = (annual_r / 100) / 12 # decimal monthly interest rate from APR
MonthlyPayment = (r * Principal * ((1+r) ** n)) / (((1+r) ** n) - 1)
return MonthlyPayment
years = int(input('Please enter number of years of loan: '))
annual_r = float(input('Please enter the annual interest rate: '))
Principal = int(input('Please enter the amount of loan: '))
print('Monthly payment: {}'.format(myMonthlyPayment(Principal, annual_r, years)))
I think in the last bit of your calculation,
/ (( 1 + r ) ** (years - 1))
you have your bracketing wrong; it should be
/ ((( 1 + r ) ** years) - 1)
I think the correct formula is this,
MonthlyPayment = (Principal * r) / (1 - (1 + r) ** (12 * years))
I cleaned up your variables some,
def get_monthly_payment(principal, annual_rate, years):
monthly_rate = annual_rate / 100 / 12
monthly_payment = principal * (monthly_rate + monthly_rate / ((1 + monthly_rate) ** (12 * years) - 1))
return monthly_payment
years = float((input('Please enter number of years of loan')))
annual_rate = float((input('Please enter the interest rate')))
principal = float((input('Please enter the amount of loan')))
print ("Monthly Payment: " + str(get_monthly_payment(principal, annual_rate, years)))
It would also be prudent to add a try-except block around the inputs.