Wednesday 20th Jan 2016
Work Sheet 1
Answer question 1 using pen and paper. Hand this up at the end of next Wednesday’s lecture.
Submit your other files as a series of .py files using Moodle by the beginning of next Wednesday’s
lecture.
Boot your machine in Linux and in your home directory, create a directory called CS6502. Change
into this directory, and create a second directory called Worksheet1. Create a file for each answer
in the following format - ws1_q2.py, ws1_q3.py and ws1_q4.py where q2, 3 and 4 correspond to
each of the questions below.
1. Provide a description of the execution of this code by listing the value of each variable as the
code executes. Also provide the final output of this code.
def getNextEven(num):
"""Some comment would go here!
"""
result = -1
if num % 2 == 0:
result = num + 2
else:
result = num + 1
return result
def main():
“””Some comment would go here!
“””
testValue = 3
testValue = getNextEven(testValue)
print(testValue)
main()
2. Write a programme that declares and initialises two variables x and y; you can initialise them
directly to some value. Write a function that takes this parameters, and using a third variable
tmp, swaps x and y, and prints the variables to output. Call the function from a main function.
3. Write a programme that calls a function called convertTemp (also write this function!) from the
programmes main function. convertTemp will take todays temperature in Cork (look up met.ie)
as a parameter and will covert from Celsius to Fahrenheit and return the value. Print this value
in the main function. The formula for the conversion is C * 9/5 + 32. You can use a hard-coded
value if you wish, or you might like to experiment with the input command; if you use input,
remember it returns a string, and you will need to explicitly convert it (see Appendix II).
4. Challenge Question - can you rewrite your answer to question 2 so that this function does not
require a third variable tmp.