Bottom Page

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)
Larz60+ wrote Jul-07-2018, 07:56 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I added for you this time, please do so on future posts.

Thank You
Quote
#2
your indentation is all messed up.
use 4 spaces per indent (PEP8).
if ... elif ... else: like:
if whatever():
    ...
elif anywho():
    ...
else:
    ...
Quote
#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

Quote
#4
Thank you ichabod801 ^_^. it worked! thank you thank you!!!!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Use range to add char. to empty string johneven 4 308 Apr-15-2019, 11:21 PM
Last Post: scidam
  Help With While Loop and Append laprus 13 554 Feb-07-2019, 07:04 AM
Last Post: perfringo
  Help with while loop creating an infinite loop. FWendeburg 3 233 Jan-30-2019, 08:28 PM
Last Post: FWendeburg
  why is this not filling my empty list? Siylo 4 247 Jan-21-2019, 05:27 PM
Last Post: Siylo
  Writing python function difficulty kirito85 5 430 Oct-28-2018, 07:34 AM
Last Post: buran
  beginning python help request (for loop) casey 3 391 Oct-26-2018, 07:26 PM
Last Post: casey
  Python Tutorial range function question HawkeyeKnight 2 382 Sep-27-2018, 09:45 AM
Last Post: ThiefOfTime
  Issues with Inserting Values into an Empty List with a While Loop TommyMer 2 464 Sep-12-2018, 12:43 AM
Last Post: TommyMer
  Help with 'while' loop! Python 3 rafaelssouzza 2 439 Sep-03-2018, 06:19 PM
Last Post: woooee
  Need of return in function if statement inside the function already returns Athul 5 561 Aug-16-2018, 10:19 AM
Last Post: DuaneJack

Forum Jump:


Users browsing this thread: 1 Guest(s)