Tuesday, 24 June 2014

Challenges

Learning aims

In this section we will be working on some tasks to enhance your problem solving skills.

Introduction

Make sure that your repository is shared with ams-bjones on github

If you need help raise an issue detailing in your repository saying what you need help with, and push the code you have so far to github.  Come and see me if you have problems doing the above.

For each task document the following in a word document:
  • Design
    • Say what it is you are trying to solve, what will your final program do?
    • Say how you are going to solve the problem - write an algorithm if you can
  • Development
    • Write a diary of how you solved the problem - take screen snips (use the snipping tool) of your code.
    • If you improve things say so - don't pretend you didn't make a mistake - say how you realized it was a mistake, and how you corrected it.
    • What variables did you use - what type of data will they be?
  • Evaluation
    • After your project evaluate it - Does it do what you said it will do, 
    • How did you check that it works
    • Does it work for all inputs?
    • What if the user does something crazy like when asked for their age enters it in words?

Task 1

Write a program which asks the user for a number then finds all the factors of that number.  A factor is a number that the number will divide exactly into.

Hint 1 - check the maths post - a number will divide into a factor exactly with no remainder

Task 2

Write a program that will take in a list of numbers.  the program will keep asking for numbers until the letter e is entered, at this point the program will calculate:
the mean (average), total, of the numbers entered.

Task 3

These are the first 6 Triangular numbers.  Write a program that will allow a user to enter a number and have it check whether a number is triangular or not.

Monday, 16 June 2014

Lists and for loops

Learning aims

To be able to use lists in a program.

Introduction


lists are declared using square brackets '[]' and assigned to a variable.

A 'for loop' iterates over a sequence.  A sequence can be a list, range or string. iterating means it works through each member of the list and does the same thing to it.

Task 1

code 1
Type in the above code.  Save and run after each print command - Explain what is happening after each print command.

Questions

  1. What is the command to add something to a list?
Look at https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range. Read down to, but not including section 4.6.2. 
  1. Write the line that would return True if 'm' was in 'yourlist'
  2. Write the line that would return True is m was not in 'yourlist'
  3. What would [2] + [2] be?
  4. What is the index of the a in alphabetlist? (alphabetlist.index('a'))
  5. What does index mean in a list? 

Task 2

The for command also works over a range.



Write and run the above program, save and run after each print command - Explain what is happening after each print command.

Questions

  1. When there is a single number given as the argument in a range function - what does it mean?
  2. When two numbers are given as the arguments to the range function what does each do?
  3. When 3 numbers are given as arguments to the range functioon what does each do?
  4. Write the Range command that would give the output [3,6,9,12,15,18]
  5. Write the Range command that would give the output [5,4,3,2,1]
  6. In the program above i stands for 'iterator'  as the loop 'iterates' accross the range.  What does iterate mean?
  7. Replace all instances of 'i' in lines 5,6,7 with 'randomWord'  What happens?  What is i?

Friday, 13 June 2014

While I make a decison

Learning aims:

To be able to do something repeatedly.
To be able to get the computer to make decisions

Introduction

Remember playing blockly?
Python has two simmilar commands - while and if.

'While' is similar to the 'repeat until' command.  Whatever is in the block will run while something is true (or not False'.

'if' is similar to the 'if path' statement in blockly.  it tests something, then if it is True, then it will do what is in the if block.

While commands do something until something is no longer true.

Task 1

We are going to create a game - Not a very good game - but you can improve it later.
code 1
Enter game1.py into your browser to play it you will need to run it from a terminal - to open a terminal - click on view >> new terminal.

points to note - line 9 on the code above is wrapped - (guesses)) is still on line 9 not on line 10.
Notice how the indents tell the computer what code belongs in each block.  lines 9-21 are all in the while block on line 8.  lines 11 and 12 belong in the if block on line 10. 

When running this we should get into the habit of running the code in the terminal.

Questions


  1. What variables are used in this code - what do they do?
  2. How does the program know when to stop the while loop?
  3. What word describes the data stored in the playing variable (True or False) (this might need looking up)
  4. How does the while loop work?  
  5. Which lines are run in the while loop which starts on line 8?
  6. How does the if statement work?  
  7. Which lines are in the if block that starts on line 19?
Write up your answers in a word document and hand in via the moodle.

Task 2

Make this a better game by making the computer choose a random number between 1 and 100.  You may need to look at  https://docs.python.org/2/library/random.html for help.  Follow Mr Jones in Github to hand This in.

Monday, 9 June 2014

Maths

Lesson Aims:

To be able to perform basic calculations using python.

Introduction

In python the first role of the computer was to be able to crunch numbers quickly - very quickly.

There are a number of mathematical operators that are built into python.

Reference - https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long-complex.  The imprtant types here for you to know are int (a number without a decimal part), and float (a number with a floating point.  Ignore complex numbers - they are something you will do if you study A' level Mathematics.  and long numbers are just as their name suggests - long. 

Task 1

Code 1
  • Create a new file called numbers.py, put the code above in it.
  • Explain what each line does in your own words.
  • What are the variables used in this program?


Task 2

There are also some more advanced mathematical functions available in a module called math.  To use the math module you need to import it first.

Code 2
Create a new file called numbers2.py
Enter the code above and run it.

  • What does the import statement do?
  • What does the octothorpe in line 4 do? (Octothorpe is the correct word for the symbol # also known as the number symbol, pound symbol, or more recently as a hash-tag)
  • What variables are used in this program - what do they each do?

Task 3

Write a program which will ask a user to enter the short two sides of a right-angled triangle and calculate the length of the long side.

Hand in

Hand in, via moodle, a word document with:
  • A link to your github repository
  • Answers to the above questions.

Remember 

To push back from c9.io
  • git status
    • Tell you the status of your repository
  • git add .
    • Adds new files to the tracker
  • git commit -a -m"put a message here"
    • Commits the changes you have made, with your message - short note about what you have done.
  • git push origin master
    • pushes the comitted changes back to the origin (where it came from - github) on the master branch. (yes you can have more branches - but dont worry about that yet.)



Tuesday, 3 June 2014

Hello You

We have learned how to make a program and run it - albeit a fairly simple one.  In this lesson we are going to change that program a little - We are going to end up with a program which asks our name, and then outputs hello - then your name.

 

Key learning:

Python


  • Creating and calling a function (def key word)
  • Using the input command (input() function)

Setting up a repository for your work and hello world

At AMS we will be using Github for storing and sharing our work.  We will use cloud9 for making and running our work.  This setup has a number of features.

Firstly this is publicly available - anyone can see your code, you can comment on each others code and help each other out.

Secondly Using these two tools together we are not reliant on the schools computers to run our software.  Our programs may run a little slower (although still blazingly fast). However we will be using a linux machine - which is what you may be using as a work computer, also there is no way that anybody can be accused of trying to tamper with the internal school system because your programs are not in the schools network.

It is possible to download your programs onto your home computers and run them there, but this is not something that we will discuss yet.


Key reference

git commands:

git status - lets you know what the status of your git repository is - should give you clues as to what you need to do next.

git add . - Note the space between the add and the dot - adds any untracked files to the git tracker

git commit -a -m"put a message here" - commits the changes you have made to the git stage - in effect saves the repository in the cloud 9 git ready to be pushed back to github.

git push origin master - pushes whatever is committed back to where it came from (origin - github) on the master branch.  

Python Commands

print() - Prints the contents of the brackets to the screen.

python file.py - runs file.py where file.py is a python program.