#๐Ÿ”’ Program to find the series

351 messages ยท Page 1 of 1 (latest)

torn wolf
#

Im a beginner and Im so lost with this homework question

rugged nimbusBOT
#

@torn wolf

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

serene mauve
#

What have you gotten so far? @torn wolf

torn wolf
#

x=float(input("Enter x: "))
n=int(input("Enter N: "))
s=0

#

is all i got ๐Ÿ˜ญ

#

i think we have to use a for loop but im not sure

serene mauve
#

Could use a for loop sure, do you have any idea how to start with that?

torn wolf
#

maybe like
for i in range(1,n+1)

serene mauve
#

That is fine

#

And could you express a single term.

#

So the first term is

+x

then

-x^2 / 2!

then

+x^3 / 3!

etc.

#

What is the pattern?

torn wolf
#

the exponent and the denominator

#

gets increased

serene mauve
#

So let us use i to denote the term number

#

What patterns do you see

#
  • and -, what is the pattern?
torn wolf
#

oh it changes every time

serene mauve
#

How could we make it clear when it is +, en when -?

torn wolf
#

could we do like even - and odd +?

serene mauve
#

if ... the term is postive, else the term is negative

serene mauve
#

What do you fill in there?

#

Given i

torn wolf
serene mauve
#

We do not have x defined

torn wolf
serene mauve
#

Ah like that

#

but still, whether the term is even or odd, has nothing to do with x

#

But with i

#

for i in range(1,n+1):

#

Here we go through each term number, where i is set to the term number each iteration.

#

1 then 2 then 3, etc.

torn wolf
#

so do we say n instead of x?

#

oh no wait its i itself

#

we use

#

i think

serene mauve
#

Yes

torn wolf
#

wait so we use i or n?

serene mauve
#

n does not change

torn wolf
#

ohhhhh

plain nimbus
#

n is just the upper limit for your i

torn wolf
serene mauve
#

When we do

for i in range(1, n + 1):
  #code

It's like doing

i = 1
# code
i = 2
# code
i = 3 
# code
...
i = n
#code
torn wolf
#

ohhh

#

so we the statement would be like

serene mauve
#

And in # code we can make use of i

torn wolf
#

for i in range(1,n+1):
if n%2==0:

serene mauve
#

!e

n = 5
for i in range(1, n + 1):
  print(n)
rugged nimbusBOT
serene mauve
#

!e

n = 5
for i in range(1, n + 1):
  print(i)
rugged nimbusBOT
torn wolf
#

ohh i see

#

so its just prints all the digits

#

till n

serene mauve
#

I assume you haven't learned about lists yet?

torn wolf
#

no we havent started yet

serene mauve
#

Ah okay. You can see range as some array/list/sequence of values like range(1, 5) is basically [1, 2, 3, 4]

#

And by doing for i in range(1, 5) it basically iterates through every value

torn wolf
#

is it possible to write

#

range(5)

#

?

serene mauve
#

The start is 0 by default

#

so range(5) is range(0, 5)

torn wolf
#

ohh can we go in reverse too? like going backwards

serene mauve
#

If youw ant yes, the parameters are range(start, stop, step)

torn wolf
#

so if i want to have 5 in the list i need say 6 instead right??

serene mauve
#

!e

print(*range(10))
print(*range(1, 10))
print(*range(1, 10, 2))
print(*range(10, 1, -1))
rugged nimbusBOT
torn wolf
#

ohh thank u this topic was very hard for me ๐Ÿ˜ญ

serene mauve
#

Yeah nws. I don't really get why teachers teach for loop before teaching lists

#

They just copy each other and do whatever is common for other languages, doesn't make sense

torn wolf
#

yeah its very confusing

serene mauve
serene mauve
#

So i' ll help a bit further

for i in range(1, n + 1):
  # If i is even
  if i % 2 == 0:
    ...
  # Else i is odd
  else:
    ...
#

With this if statement you can know if you are dealing with an even term, or odd term

serene mauve
#

i = 1

+x

i = 2

-x^2 / 2!

i = 3

+x^3 / 3!

etc.

#

We want to define something that makes it + or -

#

One easy way would be to define some multiplier multiplier and then set it to 1 or -1

#

Based on whether i is even or odd

torn wolf
#

oh

serene mauve
#

So fill in the dots

torn wolf
#

stupid doubt but can we put a for loop inside an if statement?

serene mauve
#

Yes

#

You can put an if statement in a for loop, a for loop in an if statement, an if statement in an if statement, a for loop in a for loop etc.

#

When I code I normally have an extra .py file open so I can just test this kinda stuff

#

You don't break the computer (easily) by just running some code you wrote, so just try this kinda stuff out

torn wolf
#

if i%2==0:
for j in range(1,(i*i) + 1):
term=x**j/x

im 100% this is wrong but this is what i think the blank is

serene mauve
#

Split the problem up

torn wolf
#

meaning?

serene mauve
#

Do what I said here

torn wolf
#

oh okay okay

serene mauve
#

We will first deal with - and +, then the denominator, then the numerator.

torn wolf
#

we want the sign to be -

serene mauve
torn wolf
#

i have no clue ๐Ÿ˜ญ

#

like how to mkae

#

the sign -

#

for even

serene mauve
#

Read the messages under the snippet of code

serene mauve
plain nimbus
#

I can try to help for a bit, could you show where you are currently at? I stopped following the thread ^^

torn wolf
#

no no its fine, i understood a lot

#

and thank u sm!!

torn wolf
#

so we did this

for i in range(1, n + 1):

If i is even

     if i % 2 == 0:
...

Else i is odd

     else:
...
#

and basically im trying to figure out to fill in the blanks

#

the question is this btw

plain nimbus
#

Ah okay, so you need to calculate your individual terms then

torn wolf
#

i think in if statement

torn wolf
#

so do i just make a term and set it equal to -1 under if statement

plain nimbus
#

Yeah you could do that, or just subtract it instead of adding it. All perfectly fine ways to do this.

#

So now we need to figure out how to calculate the individual values.

torn wolf
#

oh ok

plain nimbus
#

Lets disregard the first one since that is a special case we can handle later, how would you calculate one of these using the values you have so far?

torn wolf
#

theres no values cause its based on what the user inputs

plain nimbus
#

Yeah, but in your code you have variables in place of your user input. You can use these to create a formula that will calculate the values based on user inputs.

#

You already have that in your loop, with n. Which doesn't have a value until the user gives it one

#

But to write code that calculates your result you have to assume you already have values for x and n.

#

Sorry if this is confusing... I feel like my explanation is not very clear. Let me try again.

torn wolf
#

we basically

#

have to sort make like an equation

#

with n and x is what i think

plain nimbus
#

Yes exactly

torn wolf
#

u trying to say

#

?

#

ohh

plain nimbus
torn wolf
#

so will the equation sort of be like
-x^i/i

#

for even

plain nimbus
#

Well 3! != 3 but essentially you would be right

#

You equation is (-)x^i/i!
Now you just need to figure out how to write that in python.

torn wolf
#

just in case

#

?

plain nimbus
#

i! means factorial of i. Are you familiar with what factorial does?

#

It's the product of all numbers up to that number, so 3! = 1 * 2 * 3

torn wolf
torn wolf
plain nimbus
#

Yes

torn wolf
#

so in python

#

the equation (-)x^i/i!

#

would be like (-x**i)/

#

wait how do u write factorail in python

#

?

plain nimbus
#

xD

#

Yeah I was witing for this.

#

So since you are practicing loops I suggest you try writing it yourself

#

||But there is a function you can import

from math import factorial

that would also do give you a factorial||

torn wolf
#

i have no idea how factorial statement is

plain nimbus
torn wolf
#

is it like factorical()

#

im guessing tbh lol

plain nimbus
#

Yes, generally there is no need to reinvent the wheel, if there is already a working solution.
The exception to this is when learning because writing these things for yourself can teach you a lot

#

So just disregard that for now. I just wanted to show you there would be an easy way you can use in the future.

torn wolf
#

so the statement will be like

#

(-x**i)/from i import factorial???

plain nimbus
#

nono forget the import

#

You never saw that ๐Ÿ˜‰

torn wolf
#

OH ALR LOLL

plain nimbus
#

It's too early for you to worry about imports and stuff

torn wolf
#

so the factorical in python is like factorical()?

plain nimbus
#

No, python does not have a factorial by default

torn wolf
plain nimbus
#

So you would have to make it yourself

torn wolf
#

so like f=f*i

#

?

plain nimbus
torn wolf
#

so do we make another loop

#

under if the if statement

#

??/

plain nimbus
#

remember 5! = 5 * 4 * 3 * 2 (* 1)

torn wolf
#

ohh

plain nimbus
torn wolf
#

no ๐Ÿ˜ญ

#

im very clueless abt a lot of thigns in python

plain nimbus
#

Dont worry

#

Everyone starts somewhere

torn wolf
plain nimbus
#

Yes

torn wolf
#

help whats the mistake ๐Ÿ˜ญ

plain nimbus
#

well its part of the solution, but you need to do that step more than once

torn wolf
#

ohh okay

plain nimbus
#

Also f=f*i doesn't work if f is undefined

#

And I don't remember if we defined f somewhere

torn wolf
#

so do we state f=1

#

before the loop

#

?

torn wolf
plain nimbus
#

Well you can do that, but remember in a typical for loop you already get a variable that increases every iteration, so you don't need to define it beforehand.

#

for f in ... would do what you need

#

What you will need however is a variable to store your result (i guess if f was meant for that then you would be correct)

plain nimbus
torn wolf
#

oh okay

#

so the statement would go like

#
for i in range(1,n+1):
        if i%2==0:
              f=f*i
              (-x**i)/f```
rugged nimbusBOT
#

Hey @torn wolf!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
plain nimbus
#

No, you will need a 2nd for loop for your factorial.

torn wolf
#

so like another for loop

#

with a different variable or we use f itself

plain nimbus
#

Since you want to hold your result in f you need to use a new variable a the "counter" (also "index" - that's why it's usually named i) in your loop

#

However here you can't use i since your 1st loop already uses it.

torn wolf
#

oh so like for j in range(1,i+1):
f=f*j

plain nimbus
#

Yes

torn wolf
#

and then under the for j loop itself we put the equation right?

plain nimbus
#

You could even start you range at 2 range(2, i + 1) since f = f * 1 wont change your f. But thats very nitpicky and wont impact your programm overall

torn wolf
plain nimbus
#

yeah

torn wolf
#

ohh

plain nimbus
#

Also you need to "reset" f before your inner loop, the best place would be right before your 2nd for loop

torn wolf
#
for i in range(1,n+1):
        if i%2==0:
              for j in range(1,i+1):
                       f=f*j
                       (-x**i)/f```
rugged nimbusBOT
#

Hey @torn wolf!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
torn wolf
#

like do we write f=1 after the print the equation

#

?

plain nimbus
#

Yeah you just reassign 1 to f before the loop ```py
for i in range(1, n + 1):
if i % 2 == 0:
f = 1
for j in range(1, i + 1):
f = f * j
(-(x**i)) / f

torn wolf
#

ohh okayy

#

even=(-(x**i)) / f
print(even)

#

and then its over for even right?

plain nimbus
#

Also let's restructure a bit py for i in range(1, n + 1): f = 1 for j in range(1, i + 1): f = f * j if i % 2 == 0: (-(x ** i)) / f
You will need the factorial regardless of your if statement, you you can calculate it before that (avoids repetition and unnecessary indents)

torn wolf
#

or is that still possible?

#

ohh wait

#

i got it

#

i understood

plain nimbus
#

That will actually make it easier for the odd digits, since you don't need the code to calcualte the factorial again

torn wolf
#

so basically we first do the caluclate for the factrical

#

and then do the equation

plain nimbus
#

calcualte
I can't spell this word for the life of me...

torn wolf
plain nimbus
#

Yes, factorial first and then the equation.

torn wolf
#
n=int(input("Enter value of N: "))
for i in range(1, n + 1):
    f = 1
    for j in range(1, i + 1):
        f = f * j
    if i % 2 == 0:
        even=(-(x ** i)) / f
        print(even)
    else:
        odd=(+(x**i))/f
        print(odd)```
rugged nimbusBOT
#

Hey @torn wolf!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
plain nimbus
#

Looks good so far, almost there

torn wolf
#

so after this what do we do?

plain nimbus
#

Well this only gives you the values of your individual terms, not the value of the series.
So you need to add all those single values together

torn wolf
#

ohhh

#

so we need to make a statement where it adds all the even and odd right?

plain nimbus
#

I would recommend the add a variable outside of your loop that you could then use to collect all of your values. (called result or something so you know it contains your final value)

plain nimbus
#

Why 1?

#

Does your series start with 1?

torn wolf
torn wolf
#

so i thought result would also be one?

plain nimbus
#

Hmm I think the result should be 0, since your series is "empty" when you start your calculations. It only gets a value when you add to it

torn wolf
#

oh yeah true

plain nimbus
#

result here is essentialy S in your original screenshot

torn wolf
#

so do we write result=0 in the for i loop

#

or out of it

plain nimbus
#

Out of it, you wouldn't want to reset your result while calculating the series

torn wolf
#

so in the for i loop

#

do we just write result= even + odd?

plain nimbus
#

No you cannot do that, let me explain why.

#

In an if/else statement only one of the two code blocks will execute, the variable coming from the other will not be defined and raise an Exception (crash your program)

#

And even if that wasn't the case you dont want to add both values every loop

torn wolf
#

ohh

#

wait so how can we add it then?

plain nimbus
#

So you have 2 options (give me a minute)

torn wolf
#

ofc!

plain nimbus
#
# option 1: add them twice
if i % 2 == 0:
    even = (-(x**i)) / f
    result += even
else:
    odd = (+(x**i)) / f
    result += odd

# option 2: store the result in the same variable
if i % 2 == 0:
    term = (-(x ** i)) / f
else:
    term = (+(x ** i)) / f
result += term```
"+=" means in place addition.
So `a += b` is equal to `a = a + b` (just a nicer way of writing it, they are functionally the same)
torn wolf
#

can we use elif instead of else

#

cause im very confused on what situations do we use elif and else

plain nimbus
#

That would not make a lot of sense here.
elif is used if you want to check a 2nd condition. You would have to explain to me why you need that here.

#

If your number isn't even it is odd. There is no other possibility.

torn wolf
plain nimbus
torn wolf
#

so cant we just write another if statement instead of an elif

#

for another condition

#

?

plain nimbus
#

Yes you can, but then there is the possibility that both of them will run. You don't have that with if/elif - they are mutually exclusive

#

If both conditions in my example are True, then only the first if statement will run. With 2 individual ifs both of them will execute.

plain nimbus
#

Another way of saying it would be that two if statements are independent of another ||(while on the same level of indentation - nested ifs are obviously a different case)||, while an elif is always "connected" to an if statement.

torn wolf
plain nimbus
#

Yeah, elif will only check it's condition if the if statement failed it's own check.

#

Did you just add a melon to your name? xD

torn wolf
plain nimbus
#

You can also have multiple elifs in a row, each of which will only check it's condition if the previous one failed

#

And if you have an else after those that will only run if all of the previous checks failed.

torn wolf
rugged nimbusBOT
#

Hey @torn wolf!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
plain nimbus
#

Ah almost!

torn wolf
torn wolf
plain nimbus
#

You made a small error regarding indentation

#

Where your loop that calcualtes the factorial expands to your if statement

#

You don't want that, since you want to finish calculating the factorial first before moving on.

#

Otherwise you will have an "unfinished" value in f

plain nimbus
#

And the same thing (indentation) happens with your result (the term is only added to your result if your number is odd)

torn wolf
#
n=int(input("Enter value of N: "))
term=0
for i in range(1, n + 1):
    f = 1
    for j in range(1, i + 1):
        f = f * j
    if i % 2 == 0:
          term = (-(x ** i)) / f
    else:
          term = (+(x ** i)) / f
result += term```
rugged nimbusBOT
#

Hey @torn wolf!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
torn wolf
#

is it correct now?

plain nimbus
#

No ๐Ÿ˜ฆ

#

You still need to add you term to your result for every... well term (in your 1st outer for loop)

#
    if i % 2 == 0:
          term = (-(x ** i)) / f
    else:
          term = (+(x ** i)) / f
# also you should indent 4 spaces after if/else
    if i % 2 == 0:
        term = (-(x ** i)) / f
    else:
        term = (+(x ** i)) / f```
torn wolf
#

that thing has been my biggest hater since i started python

plain nimbus
#

Yeah sorry, can't help you there - that's something you have to learn when writing python

#

It replaces {} in most other languages

torn wolf
torn wolf
plain nimbus
#
x=float(input("Enter value of x: "))
n=int(input("Enter value of N: "))
result=0
for i in range(1, n + 1):
    f = 1
    for j in range(1, i + 1):
        f = f * j
    if i % 2 == 0:
          term = (-(x ** i)) / f
    else:
          term = (+(x ** i)) / f
    result += term
print(result)```
Since you're so close, this would be the correct indentation
torn wolf
#

ohhhh

plain nimbus
#

Also I removed the term=0 since it's not necessary here

torn wolf
#

OH IT HAS TO BE INSIDE THE FOR I LOOP ๐Ÿ˜ญ

torn wolf
#
n=int(input("Enter value of N: "))
result=0
for i in range(1, n + 1):
    f = 1
    for j in range(1, i + 1):
        f = f * j
    if i % 2 == 0:
          term = (-(x ** i)) / f
    else:
          term = (+(x ** i)) / f
    result += term
print(result)```

wait so this is final code right?
rugged nimbusBOT
#

Hey @torn wolf!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
plain nimbus
#

It produces the same results as my code, so I would say you're good yeah ^^

#

!e ```py
from math import factorial

def sum_of_series(n: int, x: float):
result = x
for i in range(2, n + 1):
if i % 2 == 0:
result -= xi / factorial(i)
else:
result += x
i / factorial(i)

return result

def t(n, x):
result = 0
for i in range(1, n + 1):
f = 1
for j in range(1, i + 1):
f = f * j
if i % 2 == 0:
term = (-(x ** i)) / f
else:
term = (+(x ** i)) / f
result += term
return result

for b in range(5, 8):
for a in range(3, 7):
print(f"a:{a}, b:{b}, {sum_of_series(a, b)}, {t(a, b)}")

rugged nimbusBOT
# plain nimbus !e ```py from math import factorial def sum_of_series(n: int, x: float): r...

:white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | a:3, b:5, 13.333333333333332, 13.333333333333332
002 | a:4, b:5, -12.708333333333336, -12.708333333333336
003 | a:5, b:5, 13.333333333333332, 13.333333333333332
004 | a:6, b:5, -8.368055555555557, -8.368055555555557
005 | a:3, b:6, 24.0, 24.0
006 | a:4, b:6, -30.0, -30.0
007 | a:5, b:6, 34.8, 34.8
008 | a:6, b:6, -30.0, -30.0
009 | a:3, b:7, 39.666666666666664, 39.666666666666664
010 | a:4, b:7, -60.37500000000001, -60.37500000000001
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/6SLQSV65RAWKIJI22DO7NYPU6M

plain nimbus
#

Good job ๐Ÿ‘

torn wolf
#

omg thank u sm!! i know i was being a huge pain with the stupid doubts, not only u helped with my homework, u also cleared a lot of doubts

#

again thank u sm, u literally saved my life ๐Ÿ˜ญ

plain nimbus
#

You're welcome ๐Ÿ˜„

torn wolf
#

@serene mauve thank u for ur help too!!!

plain nimbus
#

You too

#

You can use !close to close this thread

torn wolf
#

!close

rugged nimbusBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.