Skip to content

Plutonic Rainbows

Week 11

Next week's work will be solely Japanese; namely Kanji and sentence construction. I'm also going to try and get a short story finished. A busy week ahead.

Calculations and New Job

Started a new job today. Wrapping up this week's work with Python revision, so here's a simple division calculator using (float(raw_input(">"))) to get user input. I also use \n to create clean lines when terminal gives you the results.

def division(value_1, value_2):
    print "\nDividing %d / %d\n" % (value_1, value_2)
    return value_1 / value_2

value_1 = (float(raw_input(">")))
value_2 = (float(raw_input(">")))

result = division(value_1, value_2)

print "\nThe answer is %d.\n" % (result)

Putting Loops and Lists together

This seems like a lot of code (at least for a beginner like me), but it uses loops and lists so that the user can navigate around with varying outcomes.

from sys import exit

def money_room():
    print "There's plenty of money here. How much do you take?"

    next = raw_input("> ")
    if "0" in next or "1" in next:
        how_much = int(next)
    else:
        dead("Please type in something useful.")

    if how_much < 50:
        print "Nice. You're not greedy. You win!"
        exit(0)
    else:
        dead("You are greedy. You lose.")

def guard_room():
    print "There is a guard here."
    print "The guard has a magazine and is reading it."
    print "The guard is stood in front of a door."
    print "How are you going to get rid of the guard?"
    guard_moved = False

    while True:
        next = raw_input("> ")

        if next == "take magazine":
            dead("The guard takes out his gun and you're toast.")
        elif next == "distract guard" and not guard_moved:
            print "The guard has moved from the door.
            You can go through it now."
            guard_moved = True
        elif next == "distract guard" and guard_moved:
            dead("The guard gets annoyed and pulls out a gun.
            You know how this ends.")
        elif next == "open door" and guard_moved:
            money_room()
        else:
            print "I have no idea what that means."

def insanity_room():
    print "What is this place?"
    print "It is the room of insanity."
    print "Do you run for your life or simply bash your head against the wall."

    next = raw_input("> ")

    if "run" in next:
        start()
    elif "head" in next:
        dead("Well, that's the end of you.")
    elif "bash" in next:
        dead("Well, that's the end of you.")
    else:
        insanity_room()

def dead(why):
    print why, "Good Job!"
    exit(0)

def start():
    print "You are in a dark room."
    print "There is a door to right and left."
    print "Which one do you take?"

    next = raw_input("> ")

    if next == "left":
        guard_room()
    elif next == "right":
        insanity_room()
    else:
        dead("You stumble around the room until you are dead.")

start()

While Loops

while loops are a good deal more difficult as you have to visualise what is actually happening. One condition will keep running potentially forever until that condition is no longer met. The script below appends numbers to a list and stops doing so when the value is equal to or greater than 6.

i = 0

numbers = []

while i < 7:
    print "The number at the top is now: %d" % i
    numbers.append(i)

    i = i + 1
    print "Numbers are now", numbers
    print "The number at the bottom is now: %d" % i

print "The numbers:"

for num in numbers:
    print num

Easier Loops

I have found loops and lists quite difficult to follow so I've broken it down to something more simple; along with comments.

On the weather, most of the snow is melting quickly now as the temperatures move into positive figures.

#build an empty list 

elements = []

#add numbers in the range 1-10 and append them to elements

for n in range(1, 11):
    print "Adding %d to the list." % n
    elements.append(n)

#loop through the list and print the numbers    

for n in elements:
    print "These are the numbers: %d" % n

The Quietus on Leyland Kirby

The Quietus on The Caretaker's 'Take care, It's a Desert Out There'

Yet I can't help feeling a sense of quiet optimism in the melded fragments of Kirby's record. Somewhere, a tonality moves to a major key, there is still life somewhere. No matter what the textural aesthetics are telling us, the position it is coming from - the fog of dead voices, the crackle of the analogue era when futures weren't entirely lost - still maintains that something remains.

The melodies of Take Care are questioning in nature, a "What if?" in a haze of almost engulfing hopelessness. But as long as the questioning remains, so does a slight glimmer that the system around us will eventually give way, that it won't stumble on somehow still further than it already has post-2008 and certainly post-2016.

What was once simply withered has now turned rotten. There is moss growing in small patches, on the stones that mark the decaying remnants of what we once were, from the time when we had a future. But all is not yet lost.

Loops and Lists

I started looking at loops and lists today - while it's snowing outside. More snow tomorrow apparently. The new addition today is the range() function.

elements = []

for i in range(1, 6):
    print "Adding number %d" % i
    elements.append(i)


for i in elements:
    print "The numbers %d" % i

Limiting File Size to Copy with Python

This script uses some of the things that I have learnt so far; open(), read() and len(). I use import argv to pass in the file I wish to use.

The script then makes a decision using if and else as to copying it based on the file size. In this case, 290418 bytes. Once copied, the files are then closed, as this is good practice in Python.

from sys import argv

script, file_from, file_to = argv

file_in = open(file_from)
indata = file_in.read()

print "\nThe file size is %s bytes.\n" % len(indata)

if len(indata) > 290418:
    print "Sorry. The file is too large to copy."

    file_in.close()

else:
    raw_input("\nPress enter to copy file.\n")

    file_out = open(file_to, 'w')
    file_out.write(indata)

    print "All done."

    raw_input("Press enter to close the files.\n")

    file_in.close()
    file_out.close()

Percentage Calculator

This script will ask you to enter your amount and the percentage to be deducted before finally giving you the new amount.

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

def percentage(input_sum, divide, calc):
    return input_sum / divide * calc

def reduction(input_sum, answer):
    return input_sum - answer

input_sum = float(raw_input("\nEnter sum total: "))

calc = float(raw_input("\nEnter Percentage: "))

answer = percentage(input_sum, 100, calc)

reduction_total = reduction(input_sum, answer)

print "\nYou are left with £{0:.2f}\n".format (reduction_total)

Making Decisions in Python

This is a short script that shows how to use if as well as elif and else so that Python can make decisions based on the input it is given.

people = 100
cars = 30
buses = 15

if cars > people:
    print "We should take the cars."
elif cars < people:
    print "We should not take the cars."
else:
    print "We can't decide."


if buses > cars:
    print "That's too many buses."
elif buses < cars:
    print "Maybe we could take the buses."
else:
    print "We still can't decide"


if people > buses:
    print "Alright, let's just take the buses."
else:
    print "Fine. Let's stay home then."