Skip to main content
Thanks @Graipher, I missed these
Source Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158

Your code is pretty good. Change num <=100 to True and remove the un-needed num out of the while loop, and it's good. You should also expect user input to not be a number and handle it as you need.

Since comprehensions are a cleaner than map and filter, you may want to steer clear of them. Some people stick to never using them, I however use them if it makes the input clearer, such as map(str, user_input()).

You can then adjust your code slightly to get the iterative approach of:

list_ = []
while True:
    try:
        num = int(input())
    except ValueError:
        continue
    if num > 100:
        break
    if num >= 10:
        list_.append(num)

However, you may want a functional approach. This can be better as then you can pick and mix functions, but some don't like it as much.

  1. Make an infinite generator, of user input.
  2. itertools.takewhile the input is good.
  3. Filter input.
def user_input():
    while True:
        yield input()

def to_int(numbers):
    for num in numbers:
        try:
            yield int(num)
        except ValueError:
            continue

nums = itertools.takewhile(lambda n: n <<= 100, to_int(user_input()))
print('\n'.join(str(n) for n in nums if n >>= 10))

Python is multi-paradigm, and so you can mix them. And so as-long as it works and is clear, it's fine. I personally would use:

def user_input():
    while True:
        try:
            yield int(input())
        except ValueError:
            continue

def filter_numbers(numbers):
    for num in numbers:
        if num > 100:
            break
        if num >>= 10:
            yield num

print('\n'.join(str(n) for n in filter_numbers(user_input())))

Your code is pretty good. Change num <=100 to True and remove the un-needed num out of the while loop, and it's good. You should also expect user input to not be a number and handle it as you need.

Since comprehensions are a cleaner than map and filter, you may want to steer clear of them. Some people stick to never using them, I however use them if it makes the input clearer, such as map(str, user_input()).

You can then adjust your code slightly to get the iterative approach of:

list_ = []
while True:
    try:
        num = int(input())
    except ValueError:
        continue
    if num > 100:
        break
    if num >= 10:
        list_.append(num)

However, you may want a functional approach. This can be better as then you can pick and mix functions, but some don't like it as much.

  1. Make an infinite generator, of user input.
  2. itertools.takewhile the input is good.
  3. Filter input.
def user_input():
    while True:
        yield input()

def to_int(numbers):
    for num in numbers:
        try:
            yield int(num)
        except ValueError:
            continue

nums = itertools.takewhile(lambda n: n < 100, to_int(user_input()))
print('\n'.join(str(n) for n in nums if n > 10))

Python is multi-paradigm, and so you can mix them. And so as-long as it works and is clear, it's fine. I personally would use:

def user_input():
    while True:
        try:
            yield int(input())
        except ValueError:
            continue

def filter_numbers(numbers):
    for num in numbers:
        if num > 100:
            break
        if num > 10:
            yield num

print('\n'.join(str(n) for n in filter_numbers(user_input())))

Your code is pretty good. Change num <=100 to True and remove the un-needed num out of the while loop, and it's good. You should also expect user input to not be a number and handle it as you need.

Since comprehensions are a cleaner than map and filter, you may want to steer clear of them. Some people stick to never using them, I however use them if it makes the input clearer, such as map(str, user_input()).

You can then adjust your code slightly to get the iterative approach of:

list_ = []
while True:
    try:
        num = int(input())
    except ValueError:
        continue
    if num > 100:
        break
    if num >= 10:
        list_.append(num)

However, you may want a functional approach. This can be better as then you can pick and mix functions, but some don't like it as much.

  1. Make an infinite generator, of user input.
  2. itertools.takewhile the input is good.
  3. Filter input.
def user_input():
    while True:
        yield input()

def to_int(numbers):
    for num in numbers:
        try:
            yield int(num)
        except ValueError:
            continue

nums = itertools.takewhile(lambda n: n <= 100, to_int(user_input()))
print('\n'.join(str(n) for n in nums if n >= 10))

Python is multi-paradigm, and so you can mix them. And so as-long as it works and is clear, it's fine. I personally would use:

def user_input():
    while True:
        try:
            yield int(input())
        except ValueError:
            continue

def filter_numbers(numbers):
    for num in numbers:
        if num > 100:
            break
        if num >= 10:
            yield num

print('\n'.join(str(n) for n in filter_numbers(user_input())))
deleted 5 characters in body
Source Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158

Your code is pretty good. Change num <=100 to True and remove the un-needed num out of the while loop, and it's good. You should also expect user input to not be a number and handle it as you need.

Since comprehensions are a cleaner than map and filter, you may want to steer clear of them. Some people stick to never using them, I however use them if it makes the input clearer, such as map(str, user_input()).

You can then adjust your code slightly to get the iterative approach of:

list_ = []
while True:
    try:
        num = int(input())
    except ValueError:
        continue
    if num > 100:
        break
    if num >= 10:
        list_.append(num)

However, you may want a functional approach. This can be better as then you can pick and mix functions, but some don't like it as much.

  1. Make an infinite generator, of user input.
  2. itertools.takewhile the input is good.
  3. Filter input.
def user_input():
    while True:
        yield int(input())

def to_int(numbers):
    for num in numbers:
        try:
            yield int(num)
        except ValueError:
            continue

nums = itertools.takewhile(lambda n: n < 100, to_int(user_input()))
print('\n'.join(str(n) for n in nums if n > 10))

Python is multi-paradigm, and so you can mix them. And so as-long as it works and is clear, it's fine. I personally would use:

def user_input():
    while True:
        try:
            yield int(input())
        except ValueError:
            continue

def filter_numbers(numbers):
    for num in numbers:
        if num > 100:
            break
        if num > 10:
            yield num

print('\n'.join(str(n) for n in filter_numbers(user_input())))

Your code is pretty good. Change num <=100 to True and remove the un-needed num out of the while loop, and it's good. You should also expect user input to not be a number and handle it as you need.

Since comprehensions are a cleaner than map and filter, you may want to steer clear of them. Some people stick to never using them, I however use them if it makes the input clearer, such as map(str, user_input()).

You can then adjust your code slightly to get the iterative approach of:

list_ = []
while True:
    try:
        num = int(input())
    except ValueError:
        continue
    if num > 100:
        break
    if num >= 10:
        list_.append(num)

However, you may want a functional approach. This can be better as then you can pick and mix functions, but some don't like it as much.

  1. Make an infinite generator, of user input.
  2. itertools.takewhile the input is good.
  3. Filter input.
def user_input():
    while True:
        yield int(input())

def to_int(numbers):
    for num in numbers:
        try:
            yield int(num)
        except ValueError:
            continue

nums = itertools.takewhile(lambda n: n < 100, to_int(user_input()))
print('\n'.join(str(n) for n in nums if n > 10))

Python is multi-paradigm, and so you can mix them. And so as-long as it works and is clear, it's fine. I personally would use:

def user_input():
    while True:
        try:
            yield int(input())
        except ValueError:
            continue

def filter_numbers(numbers):
    for num in numbers:
        if num > 100:
            break
        if num > 10:
            yield num

print('\n'.join(str(n) for n in filter_numbers(user_input())))

Your code is pretty good. Change num <=100 to True and remove the un-needed num out of the while loop, and it's good. You should also expect user input to not be a number and handle it as you need.

Since comprehensions are a cleaner than map and filter, you may want to steer clear of them. Some people stick to never using them, I however use them if it makes the input clearer, such as map(str, user_input()).

You can then adjust your code slightly to get the iterative approach of:

list_ = []
while True:
    try:
        num = int(input())
    except ValueError:
        continue
    if num > 100:
        break
    if num >= 10:
        list_.append(num)

However, you may want a functional approach. This can be better as then you can pick and mix functions, but some don't like it as much.

  1. Make an infinite generator, of user input.
  2. itertools.takewhile the input is good.
  3. Filter input.
def user_input():
    while True:
        yield input()

def to_int(numbers):
    for num in numbers:
        try:
            yield int(num)
        except ValueError:
            continue

nums = itertools.takewhile(lambda n: n < 100, to_int(user_input()))
print('\n'.join(str(n) for n in nums if n > 10))

Python is multi-paradigm, and so you can mix them. And so as-long as it works and is clear, it's fine. I personally would use:

def user_input():
    while True:
        try:
            yield int(input())
        except ValueError:
            continue

def filter_numbers(numbers):
    for num in numbers:
        if num > 100:
            break
        if num > 10:
            yield num

print('\n'.join(str(n) for n in filter_numbers(user_input())))
Source Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158

Your code is pretty good. Change num <=100 to True and remove the un-needed num out of the while loop, and it's good. You should also expect user input to not be a number and handle it as you need.

Since comprehensions are a cleaner than map and filter, you may want to steer clear of them. Some people stick to never using them, I however use them if it makes the input clearer, such as map(str, user_input()).

You can then adjust your code slightly to get the iterative approach of:

list_ = []
while True:
    try:
        num = int(input())
    except ValueError:
        continue
    if num > 100:
        break
    if num >= 10:
        list_.append(num)

However, you may want a functional approach. This can be better as then you can pick and mix functions, but some don't like it as much.

  1. Make an infinite generator, of user input.
  2. itertools.takewhile the input is good.
  3. Filter input.
def user_input():
    while True:
        yield int(input())

def to_int(numbers):
    for num in numbers:
        try:
            yield int(num)
        except ValueError:
            continue

nums = itertools.takewhile(lambda n: n < 100, to_int(user_input()))
print('\n'.join(str(n) for n in nums if n > 10))

Python is multi-paradigm, and so you can mix them. And so as-long as it works and is clear, it's fine. I personally would use:

def user_input():
    while True:
        try:
            yield int(input())
        except ValueError:
            continue

def filter_numbers(numbers):
    for num in numbers:
        if num > 100:
            break
        if num > 10:
            yield num

print('\n'.join(str(n) for n in filter_numbers(user_input())))