ITC - Functions and Files
ITC - Functions and Files
Converting measurements.
lengthInMetres = input('Length in metres: ')
lengthInMetres = float(lengthInMetres)
lengthInFeet = lengthInMetres * 3.28084
lengthInFeet = round(lengthInFeet, 2)
Input.
lengthInMetres = input('Length in metres: ')
lengthInMetres = float(lengthInMetres)
lengthInFeet = lengthInMetres * 3.28084
lengthInFeet = round(lengthInFeet, 2)
Convert to numbers
lengthInMetres = input('Length in metres: ')
lengthInMetres = float(lengthInMetres)
lengthInFeet = lengthInMetres * 3.28084
lengthInFeet = round(lengthInFeet, 2)
Output
Each time we enter the formula, there is a risk of making
an error.
Moving it to a function means we only need to
write the formula once.
def convertToFeet(distance) :
distance = float(distance)
distanceInFeet = distance * 3.28084
distanceInFeet = round(distanceInFeet, 2)
return distanceInFeet
Converting measurements.
def convertToFeet(distance) :
distance = float(distance)
distanceInFeet = distance * 3.28084
distanceInFeet = round(distanceInFeet, 2)
return distanceInFeet
New code.
import Conversion
HelloWorld
Hello
World
DogTadpoleAxolotlTurtleChicken
Useing writelines
Reading from a file
Method one: Read in the whole file.
file = open('test.txt', 'r')
Line 1
Line 2
Line 3
Line 2
Line 2
Line 1
Line 1
Line 1
Line 2
Line 3
Line 1
Line 1
Line 2
Line 3
"Dog","Tadpole","Axolotl","Turtle","Chicken"