Difference between revisions of "Variables - Python"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Now Try)
(Now Try)
Line 38: Line 38:
  
 
=Now Try=
 
=Now Try=
Create a program to convert Pounds Stirling into Euros.
+
1) Write a program that asks the following questions:
 +
 
 +
#How many children are there?
 +
#How many sweets do they each have?
 +
#How many ducks were there?
 +
#How many sweets did each child give each duck?
 +
 
 +
It should then write a “thrilling” story:
 +
 
 +
There were [Number of children] children each with a bag containing [Number of Sweets] sweets.  They walked past [Number of ducks] ducks. 
 +
Each child gave [Number of sweets each child gives each duck] sweets to each of the ducks and ate one themself. They decided to put the
 +
rest into a pile.
 +
They counted the pile and found it contained [Number of sweets left] sweets.
 +
 
 +
 
 +
 
 +
2) Create a program to convert Pounds Stirling into Euros.
  
 
#It should ask the user to input the amount in Pounds to convert.
 
#It should ask the user to input the amount in Pounds to convert.

Revision as of 09:58, 4 March 2019

What is a Variable

A variable can be used to store a value within your program. Most languages require you to declare a variable and then assign a value to it, however python does not.

In most languages, a variable is of a specific data type and this can't change during the program. However python will determine the data type to use from the data stored in the variable.

Variables in Python

You need to give a name for the variable and then assign it a value:

counter = 100 # An integer assignment 
miles = 1000.0 # A floating point 
name = "John" # A string

You could print the current values for each variable:

counter = 100 # An integer assignment 
miles = 1000.0 # A floating point 
name = "John" # A string 
print("counter value is " + counter)
print("miles value is " + miles)
print("name value is " + name)

You can perform calculations with variables:

counter = 100 # An integer assignment 
miles = 1000.0 # A floating point 
name = "John" # A string 
counter = counter -1
miles = 1000 * 5
test = miles / 2

The arithmetic operators in python are:

Operators.png

Now Try

1) Write a program that asks the following questions:

  1. How many children are there?
  2. How many sweets do they each have?
  3. How many ducks were there?
  4. How many sweets did each child give each duck?

It should then write a “thrilling” story:

There were [Number of children] children each with a bag containing [Number of Sweets] sweets.  They walked past [Number of ducks] ducks.  
Each child gave [Number of sweets each child gives each duck] sweets to each of the ducks and ate one themself. They decided to put the 
rest into a pile.
They counted the pile and found it contained [Number of sweets left] sweets.


2) Create a program to convert Pounds Stirling into Euros.

  1. It should ask the user to input the amount in Pounds to convert.
  2. It should use the exchange rate of 1.17 Euros to 1 Pound.
  3. It should display the message:
X Pounds Stirling is Y Euros, at the exchange rate of 1.17 Euros to the Pound

Now create a version of the above program to allow the user to also input the exchange rate to use.

Extension Tasks

  1. Create a program to calculate the area of a circle
  2. Create a program to calculate the volume of a cylinder
  3. Create a program to use Pythagoras theorem