In this now you code we will learn to re-factor a program into a function. This is the most common way to write a function when you are a beginner. Re-factoring is the act of re-writing code without changing its functionality. We commonly do re-factoring to improve performance or readability of our code.
The way you do this is rather simple. First you write a program to solve the problem, then you re-write that program as a function and finally test the function to make sure it works as expected.
This helps train you to think abstractly about problems, but leverages what you understand currently about programming.
The best way to get good at writing functions, a skill you will need to master to become a respectable programmer, is to use the Write - Refactor - Test - Rewrite approach. Let's follow.
Write a program to take an input string and convert to a float. If the string cannot be converted to a float it returns the string "NaN" which means "Not a Number" We did this first part for you.
Inputs: Any value
Outputs: whether that value is a number
Algorithm:
In [ ]:
## STEP 2 : Write the program
Complete the ToNumber
function. It should be similar to the program above, but it should not have any input()
or print()
functions as those are reserved for the main program, instead the function should send variable arguments in as input, and return a value as output. In this case the function takes text
as input and returns number
as output
In [ ]:
# Step 3: write the function
## Function: ToNumber
## Argument (input): text value
## Returns (output): float of text value or "NaN"
def ToNumber(text):
# TODO Write code here
With the function complete, we need to test our function. The simplest way to do that is call the function with inputs we expect and verify the output. For example:
WHEN text='10.5' We EXPECT ToNumber(text) to return 10.5 ACTUAL: 10.5
WHEN text='threeve' We EXPECT ToNumber(text) to return 'NaN' ACTUAL: NaN
We can do this with simple print()
statements, where we simply say what we are testing, then call the function with the value.
How many do we need? Enough to cover all the possibilities in output. We only need two tests here, one for when the number can be converted and one for when the number cannot.
In [ ]:
print("WHEN text='10.5' We EXPECT ToNumber(text) to return 10.5 ACTUAL:", ToNumber('10.5'))
print("WHEN text='threeve' We EXPECT ToNumber(text) to return 'NaN' ACTUAL:", ToNumber('threeve'))
In [ ]:
## Step 4: write the program from step 2 again, but this time use the function you defined two code cells up from here.
Reflect upon your experience completing this assignment. This should be a personal narrative, in your own voice, and cite specifics relevant to the activity as to help the grader understand how you arrived at the code you submitted. Things to consider touching upon: Elaborate on the process itself. Did your original problem analysis work as designed? How many iterations did you go through before you arrived at the solution? Where did you struggle along the way and how did you overcome it? What did you learn from completing the assignment? What do you need to work on to get better? What was most valuable and least valuable about this exercise? Do you have any suggestions for improvements?
To make a good reflection, you should journal your thoughts, questions and comments while you complete the exercise.
Keep your response to between 100 and 250 words.
--== Write Your Reflection Below Here ==--