How to make series number

can someone help me? How to Write a function to generate the following series of numbers:[4,3,8,6,16,12,….,192]

Thanks

can someone help me? How to Write a function to generate the following
series of numbers:[4,3,8,6,16,12,….,192]

Without an explaination of the relationship between these numbers the
best you can do is hardwire that sequence. Normally you would not bother
if you already had an explicit list. Hmm, it looks like a sequence of
n*4, n*3 for n counting from 1. Just guessing.

Just from the phrasing (though it would be good to see the exact
question test, since this feels like homework), you’re being asked to
make a “generator” function. This is a special kind of Python function
which returns (well, "yield"s) values progressively, meaning that it can
produce the initial values before completing the entire sequence.

You know that a for-loop can iterate over any iterable? So:

for i in range(10):

sets i successively to 0, 1, 2, through to 9, which are the values
from range(10). range() is a generator function. A normal function
goes:

def f(......):
    ... do some work ...
    return final_value

so it runs to completion, then returns a result.

A generator function looks like this:

def f(......):
    ... do some work, yielding values on the way ...

for example, a simplsitc version of range():

def my_range(n):
    i = 0
    while i < n:
        yield i
        i += 1

Want you call this function it returns immediately, and you are handed a
“generator”, which is an iterable. Like this, at the interactive prompt:

>>> g = my_range(10)
>>> g
<generator object my_range at 0x104b023c0>

The generator g is iterable, so you can use it in, say, a for-loop:

for i in g:
    print(i)

though often of course you’d do this directly:

for i in my_range(10):
    print(i)

The function itself does not run until you try to iterate over it. Then
it runs until it hits a yield statement, and your iteration receives
the value passed to yield. At that point the function is paused, you
do things with the value, and then next time you iterate the function
resumes.

So possibly you are being asked to write a generator function to yield
the values from the sequence you describe. Try writing such a function
and return with code and/or questions.

I would start by writing a function with just print()ed the values
from the sequence. Make that work. Then replace the print()s with
yield statements and try using the function in a loop, with the loop
body printing the values. (Keep the print()s, actually, they will let
you see your function’s execution interleaved with the loop.

Cheers,
Cameron Simpson cs@cskk.id.au

Unless you edit your code using Photoshop, please don’t post screenshots
or photos of code.

Use markdown syntax like this;

```
# your code inside triple backtick
x  = 1
y  = 2

for i in range(10):
    print(x, y,)
```

it will show like this;

# your code inside triple backtick
x = 1
y = 2

for i in range(10):
    print(x, y)

This will help people to testing your code and give feedback :wink:

I have found the calculation pattern, so as I explain in the following picture.

and then, i have try to think and try some code, like this

n1 = 4
n2 = 1
n3 = 5
nx = 4
ny = 4

for i in range(10):
print(nx, ny, end=’ ')
nx = nx-n2
ny = nx+n3

cn = n2 + n2
n2 = n2
n2 = cn

cn2 = n3 + n3
n3 = n3
n3 = cn2

but it can’t find a bright spot, the result is much different,