Python Forum
When did the number got included in the list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When did the number got included in the list?
#11
Quote: Anyway whats the point of having a function if you need an if statement to stop the generator.
First off, it is a generator, not a function. Functions return a value and they are done. Generators return a value and hold.

The point of a generator like this is you may not always know when a sequence should end. Lets say I want a sequence of prime numbers that add up to 100 or less. I don't know when my sequence should stop exactly. I can guess, but I don't know. I could write it like this:
primes = []
total = 0
for p in prime_numbers():
    if (total := total + p) > 100:
        break
    else:
        primes.append(p)
print(sum(primes), primes)
Output:
100 [2, 3, 5, 7, 11, 13, 17, 19, 23]
Or if you prefer a while loop.
primes = []
total = 0
prime_gen = prime_numbers()
while total < 100:
    p = next(prime_gen)
    if (total := total + p) <= 100:
        primes.append(p)
print(sum(primes), primes)
My example is a bit hokey, but so is the prime_numbers() generator in your example. I think this code is supposed to demonstrate using a generator and is not meant to be used as a prime number generator.

I agree that there is probably a little language barrier here. Some of your questions are a bit awkward, but I am only proficient in one language, so you are way ahead of me. This is far from "conversational English" and many native English speakers have trouble asking questions on this forum.
Reply


Messages In This Thread
RE: When did the number got included in the list? - by deanhystad - Feb-02-2022, 07:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,660 May-01-2023, 09:06 PM
Last Post: deanhystad
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,401 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  The included URLconf 'scribimus.urls' does not appear to have any patterns in it. nengkya 0 1,098 Mar-03-2023, 08:29 PM
Last Post: nengkya
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,984 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Split a number to list and list sum must be number sunny9495 5 2,385 Apr-28-2022, 09:32 AM
Last Post: Dexty
  Divide a number by numbers in a list. Wallen 7 8,169 Feb-12-2022, 01:51 PM
Last Post: deanhystad
  Count number of occurrences of list items in list of tuples t4keheart 1 2,418 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  How do I add a number to every item in a list? john316 2 2,022 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Print the number of items in a list on ubuntu terminal buttercup 2 1,979 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Make an array of string number in a List polantas 5 3,173 May-27-2020, 07:18 AM
Last Post: buran

Forum Jump:

User Panel Messages

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