Changes

Jump to: navigation, search

Rchan sandbox

154 bytes added, 00:09, 3 November 2019
Part 2 - Pure Function and Modifiers
== Part 2 - Pure Function and Modifiers ==
In Part 1, the sum_times() function will not make any change to any of the two time objectspassed to it. It This is called a pure function. In this part, we are going to create a function which changes the value (the three attributes) of a time object based on the integer value passes passed to the function. We call such type of function a '''modifier'''. The function we are going to add is called '''change_time(time, seconds)''': where '''time ''' is the time object the function is going to modify, and '''seconds ''' is the number of seconds the function will add to the time object.
:1. Make a copy of lab7a.py and name it as lab7b.py in the ~/ops435/lab7 directory<source lang="bash">cd ~/ops435/lab7cp lab7a.py lab7b.py</source>:2. Add the following new function called '''change_time(time, seconds) ''' after the '''sum_times() ''' functioninto '''lab7b.py''':<source lang="python">
def change_time(time, seconds):
time.second += seconds
</source>
:3. Save the file.
:4. Bring up an interact interactive Python3 shell to test the new function.
::'''Testing change_time(time, seconds) with import'''<source lang="bash">
[rchan@centos7 wiki_labs]$ python3
</source>
:5. If you encounter any syntax error or exception, please fix the error before moving on to the next step.
:6. Now try to set seconds to a '''negative ''' value, ie.eg. -1800, and then call the '''change_time() ''' function.
Will it change the time1 object back to '09:50:00'? Let's try it out:<source lang="bash">
>>> format_time(time1)
1,760
edits

Navigation menu