Plutonic Rainbows

Loan Calculator

Using the Functions available for Maths, I built a simple loan calculator -including the elif and else statements so that Python can make a decision. I am sure there are much simpler ways of doing this but as I am still learning, it's all I can do right now.

# -*- coding: utf-8 -*-

def amount_to_borrow(loan_request, twelve_months):
    print "Calculating 12 months interest totalling £{0:.2f}.\n".format (twelve_months)
    return loan_request + twelve_months


loan_request = (float(raw_input("How much do you want?\nEnter Amount: ")))

monthly_payments = loan_request / 12

twelve_months = monthly_payments + 15.00 * 12

interest_payments = 15 * 12

total = amount_to_borrow(loan_request, twelve_months)

raw_input("Press Return to get your decision.\n")

credit_limit = 5000

print "The cost of your loan is £{0:.2f}\n".format (total)
print "That's £{0:.2f} in 12 monthly payments".format (monthly_payments)
print "And £{0:.2f} in Interest Payments over 12 months.\n".format (interest_payments)
print "The limit for your account is £{0:.2f}\n".format (credit_limit)

if total < credit_limit:
    print "Your loan is approved.\n"

elif total > credit_limit:
    print "Sorry. You are not approved.\n"

else:
    print "Please come into the branch to discuss it.\n"

Using Functions to do Maths

Mathematics isn't really something I'm good at but luckily, Python makes it easy when using functions. I'm also using .format to correctly show decimal places - as well as # -*- coding: utf-8 -*- so that Python can print currency symbols..

# -*- coding: utf-8 -*-
def add(a, b, c):
    print "\nADDING %d + %d + %d" % (a, b, c)
    return a + b + c

price = add(12, 12, 12);print "The price is £{0:.2f}\n".format (price)

def multiply(a, b):
    print "MULTIPLYING %d * %d" % (a, b)
    return a * b

multiply_price = multiply(10, 10);print "The price is £{0:.2f}\n".format (multiply_price)

def subtracting(a, b):
    print "SUBTRACTING %d - %d" % (a, b)
    return a - b

subtract_price = subtracting(100, 50);print "The price is £{0:.2f}\n".format (subtract_price)

def division(a, b):
    print "DIVIDING %d / %d" % (a, b)
    return a / b

division_price = division(90, 2);print "The price is £{0:.2f}\n".format (division_price)

Rewinding

This will not sink in. Have to keep looking at it over and over again. I'm just not clever enough, I think. The only way to make it stick is to look at the previous variables etc.

from sys import argv

script, input_file = argv

def print_all(f):
    print f.read()

def rewind(f):
    f.seek(0)

def print_a_line(line_count, f):
    print line_count, f.readline()

current_file = open(input_file)

print "First let's print the whole file:\n"

print_all(current_file)

print "\nNow let's rewind. Just like a tape."

rewind(current_file)

print "Let's print three lines:\n"

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

print "\n"

Defining a Function

Ended the week with revision. Have to build this into learning if I want to remember what I've studied.

Defining a function:

def print_three(arg1, arg2, arg3):
    print "Name: %r, Surname: %r, Country: %r" % (arg1, arg2, arg3)

print_three("Elle", "Macpherson", "Australia")

Copying Files

Learning how to copy files with Python. The script also checks the file size in bytes.

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Let's copy from %s to %s." % (from_file, to_file)

in_file = open(from_file)
indata = in_file.read()

print "The size is %s bytes in length." % len(indata);
print "Does the target file exist? %r" % exists (to_file)

raw_input()

out_file = open(to_file, 'w')
out_file.write(indata)

in_file.close()
out_file.close()

print "All done."