+ 1

Random "None" appears, no clue why

Hi, I've defined a function that adds up all the numbers in a list and gives you the total. Works like a charm, but each time I run it I also get "None" on the line below. Does anyone know what's causing this? Here's the definition: def listTotal(listname): f=0 total=0 while 1==1: assert(listname[f]==float(listname[f])),"Your list should only contain numbers!" total = total +listname[f] f=f+1 if f == len(listname): print (total) break Thanks in advance!

19th Apr 2019, 9:50 PM
2 Answers
+ 1
How are you calling your function? Like this? print(listTotal(your_list)) In that case, the None is the return value of your function - it has none! Either you just call the function without embedding it in a print call, or - probably more natural - you write: return total instead of print(total) in your function. You also don't need the break.
19th Apr 2019, 10:06 PM
HonFu - avatar
+ 1
To add to HonFu If you do print(print('x')) the output will be: X None Because the print function returns None.
19th Apr 2019, 11:58 PM
Sam Stoltenberg
Sam Stoltenberg - avatar