Python Forum
why is the append function gets empty on the loop?/python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: why is the append function gets empty on the loop?/python (/thread-11408.html)



why is the append function gets empty on the loop?/python - rhai - Jul-07-2018

im trying to print all the result of the comparison of each input integers in one line. But it looks like that my list becomes empty as the loop goes. any idea how to do it? here is my code so far. Thanks

f_n = 0
n_n = 0

print('Enter the first number: ' , end = ' ')
fn = input()
f_n = int(fn)

finished = False
while not finished:
    print('Enter the next number, 0 to finish: ' , end = ' ')
    nu = input()
    n_n = int(nu)
    
    if n_n != 0:
      demands = []
      if n_n == f_n:
        demands.append ('Same.')
      elif n_n > f_n:
        demands.append('Up.')
      elif n_n < f_n:
        demands.append('Down.')
      f_n = n_n
    else:
        finished = True
print (demands)



RE: why is the append function gets empty on the loop?/python - Larz60+ - Jul-07-2018

your indentation is all messed up.
use 4 spaces per indent (PEP8).
if ... elif ... else: like:
if whatever():
    ...
elif anywho():
    ...
else:
    ...



RE: why is the append function gets empty on the loop?/python - ichabod801 - Jul-07-2018

You redefine demands each time through the loop (line 15), deleting any previously stored data. You should define it once before the loop.


RE: why is the append function gets empty on the loop?/python - rhai - Jul-07-2018

Thank you ichabod801 ^_^. it worked! thank you thank you!!!!