Plutonic Rainbows

New Words

Artificial Intelligence: 人工知能.

For example, 大学から2年間ぐらい人工知能を勉強しました。

Neural Network (i.e Python's Tensor Flow): ニューラルネットワーク.

Machine Learning: 機械学習.

For example, 21世紀に機械学習の問題は増えます.

While Loop & Exit

A simple script that asks a user to guess the number. It uses a while loop that will continue to ask for user input until the condition is met. We then use exit() to break the loop.

from sys import exit

while True:
    print "Try and guess the number."
    guess = raw_input("> ")

    if guess == "7":
            print "You guessed correctly."
            exit(0)

    else:
        print "Wrong."

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()