numlist = [1, 2, 3, 4, 5]
for item in numlist:
    print(item)
print('All done!')

"""
Explanation: Introduction to Coding in Python, Part 2
Investigative Reporters and Editors Conference, New Orleans, June 2016<br />
By Aaron Kessler and Christopher Schnaars<br />
Loops
A common task in Python is looping or iterating through a collection of objects using a for loop. This extremely powerful tool lets you act on hundreds, thousands or even millions of objects with just a few lines of code. Those objects could be the characters in a string, the objects in a list, the numbers in a range, the rows in a data file, the files in a directory and so on.
Run and study this code:
End of explanation
"""


sublist = ['a', 'b', 'c']
for item in numlist:
    for subitem in sublist:
        print(item, subitem)
print('All done!')

"""
Explanation: You probably can figure out what's going on here: We have a list of five numbers. We then use a for loop to iterate over this list and print each number. But there are three things we need to take a closer look at: the word item, the colon at the end of our for statement and the four blank spaces at the beginning of the first print statement.
In the above code, item is just a variable name and has no special meaning. We could have called it anything: x, num, spam and so on. Each time Python iterates through our list, item is pointed to the next value. When Python has finished running the for loop, it promptly forgets item ever existed. In fact, if you tried to access the value of item outside the for loop, you'd get an error rather than 5. This is due to something called scope. We won't cover scope here, but in a nutshell: Because item was created as part of the for loop, it's discarded once the for loop has finished running.
The colon is used to separate the first line of the for statement from the code that will be run against each item in our list.
As for the spaces, spacing is very important in Python programs. Notice that there is no code to tell Python when it has reached the end of the for loop. This is because the spacing does that. This is why All done! is printed only once instead of five times. If we indented that last print statement, it would be printed five times: once after each value. (Feel free to try that out, if you like. We'll wait.)
You should use four spaces each time you indent your code. Technically you can use any number of spaces as long as you are consistent, but four space is standard practice among Python programmers.
You can even nest a for loop inside another for loop. In the box below, create another list called sublist with string values a, b and c. For every value in numlist, print that value and each value in sublist in this format: print(&lt;numlist value&gt;, &lt;sublist value&gt;) When you're done, print: All done!
End of explanation
"""


for num in range(1, 11):
    print(num)

"""
Explanation: Ranges
If you need a basic counter, you can create a list of numbers with Python's range function -- i.e., range(1, 10) -- and then iterate over that list. Ranges are zero-based, so when you generate a range, it will run from the first number you specify up to but not including the second number.
In the box below, iterate through a range to print the numbers 1 through 10:
End of explanation
"""


for num in range(2, 11, 2):
    print(num)

"""
Explanation: You can specify an optional third step value if you want to skip over values in your range. Write some code below to display even numbers between 1 and 10:
End of explanation
"""


numlist = [39, -36, -33, 99, 24, 40, 22, -4, -11, -20]

"""
Explanation: if ... elif ... else
You use if statements to run code only when a certain condition is True.
Run this code to generate a list of numbers:
End of explanation
"""


for num in numlist:
    if num >= 0:
        print(num)

"""
Explanation: Write some code to iterate through numlist, print positive numbers and ignore negative numbers. To write an if statement, start with the if keyword followed by the condition to be tested. Just like with for loops, you need to type a colon before you start to write your nested code.
End of explanation
"""


for num in numlist:
    if num >= 0:
        print(num, 'is positive')
    elif num > -26:
        print(num, 'is slightly negative')
    else:
        print(num, 'is very negative')

"""
Explanation: You can input one or more elif statements to test for additional conditions when your if statment evaluates to False. Each elif statement must be nested by the same amount as the if statement. You may have only one if statement but can have as many elif statements as you like to test for multiple conditions. Python tests your if and elif conditions in order. As soon as one evaluates to True, all subsequent elif statements are ignored.
Finally, you can provide an else statement at the very bottom to cover cases where all of your if and elif tests fail.
Write some code to iterate through numlist and print the value and 'is positive' for values >= 0, the value and 'is slightly negative' for values between -25 and -1, and the value and 'is very negative' for all numbers < -25.
End of explanation
"""


def clean_money_string(value):
    pass

"""
Explanation: Functions
Functions are snippets of code -- usually tiny snippets of code -- that ideally do only one thing but do it very well. As you get more and more programming under your belt, you'll want to start putting more of your code into functions. Here are a few reasons why:
<ol><li><b>Code re-use:</b> If you find yourself typing the same code over and over throughout your program, that code is a good candidate for a function. Instead of constantly retyping the same code, you type it just once and then call the same function from throughout your program.</li>
<li><b>Easier debugging:</b> If there's something wrong with your function, you have to fix your code in only one place. If you have the same code repeated throughout your program, you have to fix every instance, assuming you can find them all.</li>
<li><b>Cleaner code:</b> There's a saying that code is read a lot more often than it's written. Programs can contain thousands of lines of code. Breaking your code up into functions is an easy way to reduce complexity. That will make it easier for you and anyone else looking at your code to understand what your code is supposed to do.</li></ol>

Let's say we have a data file where 10 columns contain dollar figures. However, those dollar figures are strings rather than numbers and contain both $ currency symbols and comma separators (i.e., '$92,463.84'). We want to be able to do some math on these values, so we need to convert them to numbers. What's more, we want to round these values to the nearest whole dollar. To make it even more interesting, many of the values are missing or are invalid values, such as 'n/a' or even something like '$93,w64,092.22'. We want to replace all invalid values with a zero.
We could write 10 copies of the same code -- one for each column -- to process this data, or we could write just one function and call it 10 times.
To write a function, start with the def keyword followed by a name for your function. (Let's call ours clean_money_string.) On the same line, and in parentheses, put the names of any parameters you need to pass to your function. The parameter names are just variables and should describe the data they contain. In this case, we need to pass only one parameter. Let's call it value. Finish the line with a colon.
Just like for loops and if statements, code within a function must be nested, and a function must have at least one statement. To verify that the syntax of your function header is correct, just nest the word pass under the function header and run your code. pass is useful when you are required to input some code but don't need Python to do anything. It's also good placeholder code that you'll come back and write later.
End of explanation
"""


def clean_money_string(value):
    return_value = value.replace('$', '').replace(',', '')

"""
Explanation: The first thing our function needs to do is remove $ and commas from value. Since value is a string, we can use the replace method to help us. If you can't figure out what to do, type print(help(str.replace)) in any code box below and run that code to see how to use replace. If you get stuck, read below for the answer:
End of explanation
"""


def clean_money_string(value):
    return_value = value.replace('$', '').replace(',', '')
    try:
        return_value = round(float(return_value))
    except:
        return_value = 0
    return return_value

"""
Explanation: Your code should look something like this:<br />
def clean_money_string(value):
    return_value = value.replace('$', '').replace(',', '')
Notice that we used the replace method twice, and we've chained them together on the same line. Chaining is something very cool that you can do in Python, but don't overuse it; a bunch of chained methods can be confusing to read. Python runs chained methods in order, from left to right.
Next, we want to test whether the string can be converted to a float now that $ and commas have been removed.
To do this, we're going to introduce you to very basic error-handling using try and except. All you have to do is type try: on its own line, and then input nested code you want Python to try to run. On the next line, type except: on its own line, followed by its own nested code. The except code is run only if your try code fails.
For the try code, use Python's built-in float function to try to convert return_value, then pass this value to Python's built-in round function. If all goes well, assign this cleaned-up whole number back to return_value. If there are any problems, set return_value to 0 in your except code. Finally, type return return_value to send our cleaned-up number back to the code that called our function. Pay attention to your indentation!
End of explanation
"""


print(clean_money_string('$1,234,567.89'))
print(clean_money_string('$1,234,567.39'))
print(clean_money_string(''))
print(clean_money_string('n/a'))
print(clean_money_string('$'))
print(clean_money_string('93,w64,092.22'))

"""
Explanation: Now, let's test our function by passing in some strings. To call a function, just type the name of the function with the value we want to pass to it in parentheses. To call the function several times at ones, put each function call inside a print statement.
End of explanation
"""


