Reputation: 11
Could you help me to understand why can't have anything printed?
list = [1, 2,3,4, 5, 7, 9]
def test(list):
# Using for loop
for i in list:
d = i % 2
if d == 0:
print('odd')
save = i + " it's odd"
else:
print('even')
save= i + " it's even"
return save
print(save)
Upvotes: 0
Views: 110
Reputation: 144
just I wanted to let you know that the answer you responded too is actually mine, not Moquin. Their name shows up on there too because they edited a spelling error. Also I am creating another answer here to respond to you because I am not yet able to respond in the comment section of other peoples questions. (That is reserved for individuals who have a reputation of 50 or higher on this site). If you liked my last answer, please consider accepting it as the answer by pressing the check mark.
To answer your other question regarding preserving your work, I would consider writing your output to a list. Before, we just printed it out, but if you write it to a list you can access it later in your code. Do so like this:
lis = [1, 2,3,4, 5, 7, 9]
empty_lis = []
def test(list_):
# edit 'def' requires tab indentation
# Using for loop
for i in list_:
if (i % 2) != 0:
empty_lis.append(save)
save = ("{0} it's odd".format(i))
else:
empty_lis.append(save)
save = ("{0} it's even".format(i))
print(empty_lis)
test(lis)
or additionally, you could consider creating a log file to keep track of output and where your script breaks. It will write to a .log file in whatever path you specify.
import logging
#create log
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(message)s")
file_handler = logging.FileHandler(r'{insert path here}testlog.log')#iput file path in the curly
#braces and name the lof whatever you want. Just make sure it ends in .log
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
lis = [1, 2,3,4, 6, 7, 9]
empty_lis = []
def test(list_):
# edit 'def' requires tab indentation
# Using for loop
for i in list_:
try:
if (i % 2) != 0:
save = ("{0} it's odd".format(i))
empty_lis.append(save)
else:
save = ("{0} it's even".format(i))
empty_lis.append(save)
logger.info(" my list is{0}".format(empty_lis)) # this writes to your log
print(empty_lis)
except:
logger.exception("Did not work. See logs for details.")
test(lis)
Upvotes: 0
Reputation: 11
Brack, Thanks for your patience.
The code I was trying to modify is this: https://gist.github.com/ericrobskyhuntley/0c293113aa75a254237c143e0cf962fa
And what I want is to include a way to save the processed records when it reaches , for example, multiples of 500. It will avoid to lose work done if something goes wrong (like it did often)
Once again, thank you!
Upvotes: 0
Reputation: 144
This should work for you, let me know if it doesn't. Here's a couple of pointers on your code above:
in the line save = i + " it's odd" and save= i + " it's even" you are trying to add a string and an int -- python doesn't really like this and will throw an error. But you can use .format method to insert values into a string. Here's a link with some examples https://www.geeksforgeeks.org/python-format-function/
The return will end your function. So the way you have it will only let the for loop get the first index in the list. (index 0). So for your purposes I would just go ahead and ditch the return call.
You call your list "list", which is intuitive-especially if you are just starting out. But python has built in functions that you can call on and one of them is list. It is a good idea to think of these as "reserved words" and to not use them as variable names. Try something like "lis" or "my_list" instead. Here is a link to some built in python functions that you will see a lot. https://docs.python.org/3/library/functions.html
Lastly, to get a function to run you must call it. You can see in the code below, at the very bottom that I have test(lis). This tells python to go ahead and run the function with the input we specify (in this case it is lis)
lis = [1, 2,3,4, 5, 7, 9]
def test(list_):
# edit 'def' requires tab indentation
# Using for loop
for i in list_:
if (i % 2) != 0:
save = ("{0} it's odd".format(i))
else:
save = ("{0} it's even".format(i))
print(save)
test(lis)
Upvotes: 1