More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post
page
Python IDE Dashboard

My First HTML page!

HTML-tagAre you ready to set up your first webpage?
All you have to do is follow the following 5 easy steps…

Step 1: Getting Ready

  • Go to your files (e.g. “My Documents”) and create a new folder called “My Website”
  • Inside this folder create a subfolder called “Images”

Step 2: Let’s Get Started

Open your favourite HTML or text editor. Not sure which one? We recommend NotePad++.
Type the following code:

<!DOCTYPE html>
<html>
<head>
	<title>My First Web Page</title>
</head>
<body bgcolor="#BBCCFF">
<h1>Welcome to my first webpage</h1>
<p>I hope you will like this page.</p>

</body>
</html>

Step 3: Saving Your Work

Save this file as index.html in your website folder. (The folder you created in step 1).

SaveAs-Index-html
Notice the file extension: .html Make sure you save your file with this extension, and not as a .txt file!

Step 4: Let’s See It

MyWebsite-Folder

Go to your website folder and open (double click on) your file called “index.html”. It should now appear in your web browser.

Step 5: And Now?

  • Go back to your HTML code and change the text for your first paragraph.
  • Save your file.
  • Reopen it in your web browser, or if you still have it in your web browser, press the F5 key on your keyboard to refresh the page. Your new text should now appear. If not make sure you have saved your code (e.g. in Notepad++) and then in your web browser press the F5 key again.

My First Python Game – Guess the Number

Sometimes the best way to get started with a new programming language is to do some reverse engineering. This consists of looking at an existing piece of code to find out how it works.

So let’s look at the piece of code for a fairly basic yet fully working Python game of “Guess the Number”. Look at the code. Look at the syntax. Try to understand what this code do, line by line. Tweak the code to see if you can adapt this game.

Here is the code.

import random

#Generate a Random Number between 0 and 100 and store it as 'numberToguess'
numberToGuess=random.randint(0,100)

userGuess=-1

while userGuess!=numberToGuess:
    #Get the user to enter a number using the 'input' function and convert in to an Integer suing the 'int' function
    userGuess=int(input("Guess the number (between 1 and 100)"))
    
    #Compare this number, userGuess, with the numberToGuess - Display the right message if the userGuess is greater than, lower than or equal to the numberToGuess
    if userGuess>numberToGuess:
        print("Too high!")
    elif userGuess<numberToGuess:
        print("Too low!")
    elif userGuess==numberToGuess:
        print("Good guess, the number to guess was " + str(numberToGuess) + " indeed.")
        #End of game - exit the while loop
        break

Python Code

Your Challenge:

Could you change this code to count how many attempts you had at guessing the right number. Once you guess the correct number, the program should tell you how many guesses you used.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

JavaScript: When 2+2 = 22!

In our previous blog post we looked at how to retrieve values from various HTML form controls.

For instance the following JavaScript code shows you how to retrieve the value typed in a textbox.

See the Pen DwahE by 101 Computing (@101Computing) on CodePen.

So let’s apply this to a tiny “online calculator” project to perform a sum (+ operator) of two numbers. We would need:

  • two textboxes for the user to enter the two numbers to be added.
  • one textbox to display the result of adding the two numbers.

See the Pen FCGDH by 101 Computing (@101Computing) on CodePen.

This JavaScript code retrieves the values from the textboxes and add them together… But when you try to add 2 and 2 together the outcome is not 4 but 22!!!

This is because textbox entries are providing values as text (String Data Type) and not number.

If we want to perform mathematical operations we need to convert this text into numbers.

There are two main types of numbers: Integer and Real.

  • Integers are whole numbers with no decimal points such as 3 or -7 or 101.
  • Reals (aka float) are numbers which can have decimals such as 3.1415 or -4.99.

To convert text into an integer we can use the parseInt() function as follows:

var st="2"
var myNumber=parseInt(st);

To convert text into a real/float we can use the parseFloat() function as follows:

var st="3.14"
var myNumber=parseFloat(st);

So the code for our basic online calculator should be as follows:

See the Pen vpHKs by 101 Computing (@101Computing) on CodePen.

JavaScript: Accessing HTML Form Controls

Retrieving value from a Textbox:

See the Pen DwahE by 101 Computing (@101Computing) on CodePen.

Retrieving value from a Textarea:

See the Pen tFJiI by 101 Computing (@101Computing) on CodePen.

Retrieving value from Checkboxes:

See the Pen asiqt by 101 Computing (@101Computing) on CodePen.

Retrieving value from Radio Buttons:

See the Pen dpJtD by 101 Computing (@101Computing) on CodePen.

Retrieving value from a Dropdown List:

See the Pen yGofD by 101 Computing (@101Computing) on CodePen.

JavaScript – How to read through a text file…

In this project we are looking at how to read through a text file using JavaScript. We will store a list of pupils in a text file. We are going to read through this list display its content on the webpage, one line at a time.

Step 1:

Using notepad, create a CSV file called ClassList.csv (Instead of using comma we use a pipe | as a separator)

John|Lennon|Male
Katy|Perry|Female
Robbie|Williams|Male
Amy|MacDonald|Female

 

Step 2:

Create an HTA application (See our blog post about HTA application).

Step 3:

Add the following code somewhere in the body section of the page:

<input type="button" onClick="Javascript: displayClassList();" value="Display Class List" />
<div id="searchResults"></div>

Step 4:

Add the following Javascript  code in the head section of the page (using as <SCRIPT> tag):

function displayClassList() {

var path="ClassList.csv"
var fso = new ActiveXObject('Scripting.FileSystemObject'),
iStream=fso.OpenTextFile(path, 1, false);

document.getElementById("searchResults").innerHTML="";

while(!iStream.AtEndOfStream) {
    var line=iStream.ReadLine();
    document.getElementById("searchResults").innerHTML += line + "<br/>";
}

iStream.Close();
}

The purpose of this code is to stream through the text file, which means to read it one line at a time.

Your challenge


Use and tweak this code to only display the boys from your class list.

HTML Applications (.HTA)

Have you ever heard of HTA files aka HTML Applications? If you not here is what you need to know about HTA:

  • HTA files look very similar to HTML files but when you save them you need to give them the .hta extension.
  • HTA understands everything the browser understands including HTML, Javascript and CSS code.
  • Microsoft described an HTA as running much like an .exe file.
  • HTAs will run from your system so they are not bound by security or privacy concerns that are found on the Internet.
  • Which means for example that HTA can be used to read and write on local text files using Javascript.
  • HTA will appear in a window that can be customised (Icon, toolbar…), so they look more like a Desktop application rather than a webpage.
  • HTA applications need the following tag in the <HEAD> section of the code:
<HTA:APPLICATION
border="thin"
borderStyle="normal"
caption="yes"
icon="./favicon.ico"
maximizeButton="yes"
minimizeButton="yes"
showInTaskbar="no"
windowState="maximize"
innerBorder="yes"
navigable="yes"
scroll="auto"
scrollFlat="yes" />

Why should you try HTA applications when learning to code using JavaScript?

  • For all your HTML/Javascript projects where you need to bypass browser security restrictions such as accessing local text files.
  • If you want to create Desktop applications (rather than webpages). You can build them in HTML, CSS, Javascript but the application will look like a desktop application with its own window. It will not be displayed using the web browser.
Tagged with: , , ,

Python Patterns

Here is a quick challenge to focus on the use of loops, nested loops and string manipulation using Python.

Let’s look at the following code:

for i in range(0,9):
    st=""
    for j in  range(0,i):
        st = st + " "
    st = st + "#"
    print st

btnTryCodeOnline_Over

This code uses nested “for” loops (a loop within a loop). The output will be as follows:

PythonPattern-1

Note that there are sometimes more than one solution to the same problem. Look at the following code which makes more effective use of string concatenation techniques:

for i in range(0,9): 
    print (" " * i) + "#"

btnTryCodeOnline_Over

This code would generate exacly the same outcome. It is more effective as it performs the same task using less instructions and iterations.

You can now adapt these techniques to try to produce the following patterns…

Python Patterns

Tagged with: , ,

How to create a bouncing ball in Scratch

BouncingBallStep 1 – Change your main sprite to a ball

BasketballSpriteStep 2 – Add the following script to your sprite:  BouncingBall_Script

This tutorial is available as a pdf: How to create a bouncing ball in Scratch

See how to create a Pong game using this code: http://scratch.mit.edu/projects/10128515/#editor

Tagged with: , , ,

What’s an algorithm and what is pseudo code?

Check this video/animation from Ted Ed to understand what we mean by algothirm and pseudo code.

Source: http://ed.ted.com/lessons/your-brain-can-solve-algorithms-david-j-malan#watch

 

Tagged with: ,

How to score points when your sprite collides with another sprite (in Scratch)

Scratch Tutorial -How to score points when your sprite collides with another spriteThis tutorial is available as a pdf.

It focuses on how to create a score variable for your game and how to score point when your sprite catches/touches/collides with other sprites.

Download this tutorial as a pdf:

How to Score Points when colliding with objects

Tagged with: ,