In [1]:
print ("Hi") #you can have spaces after print...didn't know that
In [2]:
#raise SystemExit
In [3]:
principal = 1000
rate = .05
numyears = 5
year = 1
print("year", "principal")
#I put this in a function since I don't want to change the values
#rather than working with global variables, fun uses local ones.
def fun(principal, rate, numyears, year):
while year <= numyears:
#principal = principal*(1+rate)
#print(year, principal)
principal*=1+rate#c++ style also works
#3d integer right aligned in column of width 3
#0.2f float with 2 digits after decimal point
#print("%3d %0.2f" % (year, principal))
#a more modern way uses the format() function
#print(format(year,"3d"), format(principal,"0.2f"))
#strings also have a format method.
#{argument passed to format() method, format specifier}
print("{0:3d} {1:0.2f}".format(year,principal))
year += 1
fun(principal,rate,numyears,year)
In [4]:
print(principal, rate, numyears, year)
In [5]:
#just want to see if python can do *= like c++
a = 1
for i in range(1,11):
a *=2
print(a)
In [6]:
#file input/output
#read from file
for line in open('foo.txt'):
print(line, end = "")
#added end argument to get rid of spaces after each line
In [8]:
#output to file
f = open("out.txt", 'w'),#open file for writing
while year<= numyears:
principal*=1+rate
print("{0:3d} {1:0.2f}".format(year,principal),file = f)
#f.write("%3d %0.2f\n" % (year, principal)) is equivalent
year+=1
In [11]:
#read user input
#import sys
#dir(sys)
#sys.stdout.write("Enter your name: ")
#name = sys.stdin.readline()#this doesn't work?
name = input("enter your name: ")
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: