0

This is my code and I want to save the output of this program into the text file.

#Compound Interest Calculation
#The formula for annual compound interest, including principal sum, is:
"""A = P*(1 + r/n)**(nt)
Where:
A = the future value of the investment/loan, including interest
P = the principal investment amount (the initial deposit or loan amount)
r = the annual interest rate (decimal)
n = the number of times that interest is compounded per year
t = the number of years the money is invested or borrowed for"""

# File Objects
with open('py.data.txt','w') as file: #in write mode
    file.write(str(cName))

cName = int(input("Enter the customer name:"))
print("The cutomer name is" , cName)
date_entry =  int(input("Enter a date in YYYY-MM-DD format:"))
print("The investment date is" , date_entry)
P = float(input("Please enter the amount you would like to invest:"))
print("The interest rate varies with the number of years")
t = float(input("please enter the number of years the money is inested or borrowed for:"))
print("The interest rate varies with the number of years")
if (t>=10):
    r = 0.05
    print("interest rate=0.05")
elif (t>=5):
    r = 0.03
    print("interest rate=0.03")
else:
    r = 0.02
    print("interest rate=0.02")

n = float(input("Please enter the number of times that interest is compounded per year:"))
A = P*(1 + r/n)**(n*t)
print("The value of investment is £" , A)
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Hadia
  • 1
  • 1
    You seem to already be doing some writing, but you're writing a variable that doesn't exist at the time you call `write` (`cName` isn't defined until the next line). Do you want to write all your output to `py.data.txt`, or somewhere else? – ShadowRanger Dec 21 '20 at 18:41
  • Yes, I want to write all my outputs to py.data.txt. And Do I call write (cName) at the end of my program? – Hadia Dec 21 '20 at 19:15
  • Just indent everything inside the `with` block and change each `print(...)` to `print(..., file=file)` and that will do it; in modern Python, `print` can take `file` as a keyword argument, and will use it instead of `sys.stdout` when it's provided. With that clarification, this is a dupe of [Correct way to write line to file?](https://stackoverflow.com/q/6159900/364696) (the accepted answer with `write` is inferior to the `print` solution in this case). – ShadowRanger Dec 21 '20 at 19:24

2 Answers2

0

If you simply want to save the print statement in another txt file you may run the python code with like this:

python file.py > output.txt

This will save the console output in output.txt in the directory where your code is running

Prateek Jain
  • 231
  • 1
  • 11
-1
cName = int(input("Enter the customer name:"))
print("The cutomer name is" , cName)
date_entry =  int(input("Enter a date in YYYY-MM-DD format:"))
print("The investment date is" , date_entry)
P = float(input("Please enter the amount you would like to invest:"))
print("The interest rate varies with the number of years")
t = float(input("please enter the number of years the money is inested or borrowed for:"))
print("The interest rate varies with the number of years")
if (t>=10):
    r = 0.05
    print("interest rate=0.05")
elif (t>=5):
    r = 0.03
    print("interest rate=0.03")
else:
    r = 0.02
    print("interest rate=0.02")

n = float(input("Please enter the number of times that interest is compounded per year:"))
A = P*(1 + r/n)**(n*t)
print("The value of investment is £" , A)

**PyData =  open('py.data.txt','w') as file: #in write mode
PyData.write(cName,date_entry,P,t,A )  #Data type is irrelevant...  I don't know if it is a comata or a plus
PyData.close()** 

The last part is from a project of mine so it should work, and also you referenced cName before it was assigned. Kind Regards

Henrik
  • 1
  • I tried your lines of code and somehow and it shows me the invalid syntax for "as file". Thanks for the help. – Hadia Dec 21 '20 at 19:25