[Python] Get the number of decimal places and print out the number of decimal places

Asked 2 years ago, Updated 2 years ago, 133 views

Hi, everyone. I'm asking you a question because I didn't get it while studying Python.

You want to receive a decimal number and print it out as many as you wrote in the title. I want to get an answer by making a negative number, zero, or enter key exception using the while statement It's frustrating because I'm still a beginner and I don't know how to approach and how to make it.

while-statement python primes

2022-09-20 22:23

2 Answers

>>> import sympy
>>> sympy.isprime(3)
True
>>> sympy.isprime(10)
False
>>> def get_n_primes(n):
...     ...     primes = []
...     ...     i = 1
...     ...     while len(primes) != n:
...         ...         if sympy.isprime(i):
...             ...             primes.append(i)
...         ...         i+=1
...     ...     return primes

>>> get_n_primes(3)
[2, 3, 5]
>>> get_n_primes(10)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
>>> N = int(input("how many primes you want? :"))
how many primes you want? :33

>>> l = get_n_primes(N)
>>> l
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137]


2022-09-20 22:23

def inc_from(n):
    while True:
        yield n
        n += 1

def take(n, iter):
    i = 0
    while i < n:
        yield next(iter)
        i += 1

def filter(f, iter):
    for x in iter:
        if f(x):
            yield x

def prime(iter):
    p = next(iter)
    yield p

    # # in python2
    #for v in prime(filter(lambda n: n % p != 0, iter)):
    #    #    yield v

    # # in python3
    yield from prime(filter(lambda n: n % p != 0, iter))

N = 100
print([x for x in take(N, prime(inc_from(2)))])


2022-09-20 22:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.