Programming for GCSE · 01.05.2014 · TypeError: object of type 'float' has no len() >>> >>>...
Transcript of Programming for GCSE · 01.05.2014 · TypeError: object of type 'float' has no len() >>> >>>...
Teaching London Computing
Programming for GCSEVariables and Input
|Teaching London Computing
MJD KCL 2014
Teaching London Computing
Declaring variables• In some languages VARIABLES (and other elements like
functions) have to be DECLARED• You have to start by IDENTIFYING (naming) them and
specifying their TYPE, (integer, string, float, list, array…) etc• In python declaration is implicit; when a variable is used,
python has a good guess and fixes its type by the value it is ASSIGNED
• And actually you can change the type by assigning a value of a different type
• If you haven’t yet given a variable a value, you cannot use it, python doesn’t yet know what type it is; you’ll get an error.
• We have already met integers (whole numbers) and strings (text)
• A float (floating point number) is a decimal, and we will meet lists and arrays later
variable, declare, assign, type, value, integer, string, list, array, float
Teaching London Computing
Check it out… in the shell>>> apples = 3>>> apples = apples -1>>> apples2>>> len (apples)Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>len(apples)
TypeError: object of type 'int' has no len()>>>
The function len only works on strings, and apples is obviously a number, an integer (whole number) in fact.
I ate one!
misconception ALERT!!!
In python = does NOT mean equals, it denotes ASSIGNMENT
apples = (is given the value ) 3
Equals is actually = = !!!
apples = apples - 1
is NOT an equation!!!!!
You can do arithmetic with apples but you cannot find its length.
Teaching London Computing
More stuff to check in the shell>>> apples = 3>>> apples = = 3True>>> apples = apples - 0.3>>> apples2.7>>> len (apples)Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>len (apples)
TypeError: object of type 'float' has no len()>>> >>> apples = "Pink Lady">>> len (apples)9>>> apples= apples-1Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>apples= apples-1
TypeError: unsupported operand type(s) for -: 'str' and 'int'
Type in the lines that begin >>>
And see if you can explain what happens
Teaching London Computing
More stuff to check in the shell>>> apples = 3>>> apples = = 3True>>> apples = apples - 0.3>>> apples2.7>>> len (apples)Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>len (apples)
TypeError: object of type 'float' has no len()>>> >>> apples = "Pink Lady">>> len (apples)9>>> apples= apples-1Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>apples= apples-1
TypeError: unsupported operand type(s) for -: 'str' and 'int'
a booleanthe statement appes is equal to 3 is
either True or False
I took a big bite out of the apple! apples is not an integer
now. We have given it the value 2.7 by subtracting
0.3, so it is a float(floating point number
or decimal)
apples is now a stringSo len (apples) works fine!
But we cannot do arithmetic
with it
Teaching London Computing
Fruit Salad in IDLE• Write these progams into IDLE and run
them… see if you can explain what happens
• Add the line print ("I have got", fruit, "pieces of fruit")
#FruitSalad1apples=3pears=5bananas=7oranges=4fruit=apples+pears+oranges+bananasprint (fruit) Take a note of this
construction; it’s important
If you change this line toPrint (“fruit”)
What will happen? Why?
The colours should give you a clue!
Print (“fruit” )
# comment
Teaching London Computing
Fruit Salad continued
• Change your program, and resave it with a new name
#FruitSalad2apples=3pears=5bananas=7oranges=4grapes= 27fruit=apples+pears+oranges+bananasprint (fruit)print ("I have got" , fruit, "pieces of fruit" )
What happens?
Does Python know grapes are fruit?
DOH!
print ("I have got" , fruit, "pieces of fruit and a bunch of grapes!" )
Teaching London Computing
Even More Fruit Salad
• Change your program, and resave it with a new name
#FruitSalad3apples=3pears=5bananas=7oranges=4grapes= 27bananas= bananas-1 # one got eatenfruit=apples+pears+oranges+bananasprint (fruit)print ("I have got" , fruit, "pieces of fruit" )
What happened?
Why?
Teaching London Computing
Even More Fruit Salad
• Change your program, and re-save it with a new name
#FruitSalad4apples = 3pears = 5bananas = 7oranges = 4grapes = 27bananas = bananas - 1 # one got eatenfruit = apples+pears+oranges+bananaspears = pears - 2 #over-ripe; threw them awayprint (fruit)print ("I have got" , fruit, "pieces of fruit" )
Is this what you expected?
What happened?
Why?
Compare this to how a
SPREADSHEET works
Misconception Alert!!!!!
Teaching London Computing
Values are assigned only WHEN PROGRAMMED TO DO SO
#FruitSalad3apples = 3pears = 5bananas = 7oranges = 4grapes = 27bananas = bananas - 1 # one got eatenfruit = apples+pears+oranges+bananaspears = pears - 2 #over-ripe; threw them awayprint (fruit)
print ("I have got" , fruit, "pieces of fruit" )Compare this to
how a SPREADSHEET
works
Misconception Alert!!!!!
fruit gets assigned its value here
When pears still has the value 5
Teaching London Computing
Values are assigned WHEN PROGRAMMED TO DO SO
#FruitSalad3apples = 3pears = 5bananas = 7oranges = 4grapes = 27bananas = bananas - 1 # one got eatenfruit = apples+pears+oranges+bananaspears = pears - 2 #over-ripe; threw them awayprint (fruit)
fruit = apples+pears+oranges+bananas
print (fruit)
print ("I have got" , fruit, "pieces of fruit" )
Compare this to how a
SPREADSHEET works
Misconception Alert!!!!! fruit gets assigned its
value hereWhen pears still has
the value 5
It will be assigned a new value only if there is an
instruction to make it do so
Teaching London Computing
LEFT AND RIGHT MATTERS!!!!
• apples = 6• pears = 7• apples = = pears will get False• apples = pears # check it to see• print (apples)• print (pears)
• pears==apples gets what?
• HOW do you swop the values of two variables????
will assign the value of pears to apples… check it out
And you cannot just swop them over!
Misconception Alert!!!
Teaching London Computing
User input
• Another way of assigning a value to a variable is to get the user to give it
• Eg name = input (“please write your name ”)• You can test this in the shell or include it in a program,
so when python gets to this point it will ask for user input and assign the input value to the variable
• So if you tell it to print that variable, it will print the value that the user has input
• Try it…. in the shell… then write a program to get a user’s name and tell them how many letters it has…
>>> name = input ("Please write your name " )Please write your name Margaret>>> print (name)Margaret>>>
Leave a space to make a gap before the input
Teaching London Computing
BUT… anything input from a keyboard is actually a STRING, so…• Keyboard input is text, and if necessary
must be converted into integers• You can do this in two stages, or all-in-one
>>> age = input ("How old were you last birthday? ")How old were you last birthday? 21>>> age = int(age)
>>> #alternatively>>> age= int(input ("How old were you last birthday? "))How old were you last birthday? 21>>>
Check this works by using age = age+1Which will give an error if the value of age is a string
Misconception Alert!!!
Teaching London Computing
Variables and assignment : Summary• A variable is a name for a value that can be changed,
a bit like a cell in a spreadsheet• The type can be an integer, float, string boolean, list ….etc etc• The type can be changed, • If you forget what type a variable is and try to do inappropriate things to
it, you will get errors• The user can give a value to a variable• Keyboard input is always a “string” but you can change it• A variable value does NOT update itself (so its NOT like a spreadsheet)• It only changes when a new value is assigned to it.• = means assignment and variable = value is NOT an equation• Left and right matters• = = means equals and is a boolean it returns True or False
• There are also• <= , <• >= , >• != (is NOT equal to)
Misconception Alert!!!
There are MANY misconceptions possible with variables
Teaching London Computing
ERRORS these are some you will probably come across today
• We have looked at errors of type• Which you get it you use a function on the wrong type of variable• There are other errors…• You will gradually learn to make sense of them>>> pring("Hello" )Traceback (most recent call last):File "<pyshell#35>", line 1, in <module>pring("Hello")
NameError: name 'pring' is not defined>>> • This is another common error, when you mistype something, it tells you
that you are using a function or a variable that has not been defined• >>> print ("hello world) •• SyntaxError: EOL while scanning string literal• >>> • Another common error… I didn’t finish the string, it got to the
EndOfLine without finding the ”
Python is also very fussy about
indentation…As you will find out!
When you fix one error, it may tell you about
another… it only bothers with the first one it sees.