Python Forum
why is the append function gets empty on the loop?/python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why is the append function gets empty on the loop?/python
#1
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)
Reply
#2
your indentation is all messed up.
use 4 spaces per indent (PEP8).
if ... elif ... else: like:
if whatever():
    ...
elif anywho():
    ...
else:
    ...
Reply
#3
You redefine demands each time through the loop (line 15), deleting any previously stored data. You should define it once before the loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Thank you ichabod801 ^_^. it worked! thank you thank you!!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can anyone spot why this function is returning an empty list? hhydration 2 1,840 Nov-18-2020, 06:16 AM
Last Post: deanhystad
  Why does this function print empty list? hhydration 1 1,520 Oct-28-2020, 02:03 AM
Last Post: deanhystad
  How can I run a function inside a loop every 24 values of the loop iteration range? mcva 1 2,129 Sep-18-2019, 04:50 PM
Last Post: buran
  Help With While Loop and Append laprus 13 5,652 Feb-07-2019, 07:04 AM
Last Post: perfringo
  Issues with Inserting Values into an Empty List with a While Loop TommyMer 2 3,754 Sep-12-2018, 12:43 AM
Last Post: TommyMer
  python append dwiga 5 4,351 Sep-04-2017, 07:27 AM
Last Post: dwiga
  While loop within a Function (2.7) tuffgong 3 3,501 Jun-28-2017, 05:54 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020