I've got a code fragment:

#!/usr/bin/env python
 
import sys

num_lst = []
num_circ = []
 
 
while True:
    print "Type number or [e]nd operation"
    inpt = sys.stdin.readline()
    print inpt
    if inpt == 'e':
        sys.stdin.flush()
        print "\n"
        break
    else:
        num_lst.append(inpt)
        sys.stdin.flush()
        for i in num_lst:
            if isinstance(i, int):
                num_circ.append(i)
            else:
                pass
        print "Typed %d number\n" % len(num_circ)

The part where the

num_lst.append(inpt)

function is invoked, works not as i'd like to.

That chunk works fine:

print "Type number or [e]nd operation"
inpt = sys.stdin.readline()
print inpt

the input is accepted and printed out, but here:

else:
        num_lst.append(sys.__stdin__)
        sys.stdin.flush()
        for i in num_lst:
            if isinstance(i, int):
                num_circ.append(i)
            else:
                pass
        print "Typed %d number\n" % len(num_circ)

this chunk of code doesn't work, because no value is appended into num_lst list.

I'm quiet disoriented. How to append the sys.stdin buffer into list in most pythonic way?

I would use the interactive function input something like this snippet

prompt = "Enter a number or 'e' to end "
user_list = []
while True:
   value = input(prompt)
   if value.lower().startswith('e'):
      break
   user_list.append(value)

print(user_list)

Beware that input() returns a string, not a number, so you will need to convert if you really need numbers.

I would use the interactive function input something like this snippet

prompt = "Enter a number or 'e' to end "
user_list = []
while True:
   value = input(prompt)
   if value.lower().startswith('e'):
      break
   user_list.append(value)

print(user_list)

Beware that input() returns a string, not a number, so you will need to convert if you really need numbers.

I know about input() and raw_input() but i'd like to use sys.stdin, just don't know why this buffer isn't appended into list.

I see several problems:

  • inpt is a string, so your line 21 will never be true. If you want integer types (as your code indicates), use the isdigit() method on strings. See also point 3
  • Your lines 20 and further should not be in the loop. Indentation counts!
  • Your line 13 will never succeed in breaking the loop. Better if at line 11: inpt = sys.stdin.readline().strip() or line 13:if inpt.strip().lower().startswith('e') or some combination

In addition, I find myself typing your inpt with a 'u' in it to spell a word. In general, it is good practice to use fully spelled words. Python best practices suggest is that functions/methods and variables are spelled_with_underbars and class names are spelled in CapitalCamelCase. By doing so, you avoid the need to remember the way things are spelled and need only remember the name (or concept) itself. Exceptions being: Loop variables may be i, x, etc. and of course if the "natural" name for something is a keyword or common Python name, you must find an alternative such as from_limit (avoids keyword 'from'), user_input (avoids global function 'input') or max_value (avoids global function 'max').

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.