#๐ Program to find the series
351 messages ยท Page 1 of 1 (latest)
@torn wolf
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.
What have you gotten so far? @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
Could use a for loop sure, do you have any idea how to start with that?
maybe like
for i in range(1,n+1)
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?
So let us use i to denote the term number
What patterns do you see
- and -, what is the pattern?
oh it changes every time
How could we make it clear when it is +, en when -?
could we do like even - and odd +?
if ... the term is postive, else the term is negative
im not sure but like is it
for i in range(1,n+1):
if x%2==0:
We do not have x defined
but like x is given by user right?
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.
Yes
wait so we use i or n?
n does not change
ohhhhh
n is just the upper limit for your i
ohh thank u
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
And in # code we can make use of i
for i in range(1,n+1):
if n%2==0:
!e
n = 5
for i in range(1, n + 1):
print(n)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 5
002 | 5
003 | 5
004 | 5
005 | 5
!e
n = 5
for i in range(1, n + 1):
print(i)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 1
002 | 2
003 | 3
004 | 4
005 | 5
I assume you haven't learned about lists yet?
no we havent started yet
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
ohh can we go in reverse too? like going backwards
If youw ant yes, the parameters are range(start, stop, step)
ohh but like if i say range(5), it will only print till 4
so if i want to have 5 in the list i need say 6 instead right??
!e
print(*range(10))
print(*range(1, 10))
print(*range(1, 10, 2))
print(*range(10, 1, -1))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | 0 1 2 3 4 5 6 7 8 9
002 | 1 2 3 4 5 6 7 8 9
003 | 1 3 5 7 9
004 | 10 9 8 7 6 5 4 3 2
ohh thank u this topic was very hard for me ๐ญ
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
yeah its very confusing
Just remember this part for now
alr ๐ซก
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
ohh ok ok
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
oh
So fill in the dots
stupid doubt but can we put a for loop inside an if statement?
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
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
Split the problem up
meaning?
^
oh okay okay
We will first deal with - and +, then the denominator, then the numerator.
so under the if statement
we want the sign to be -
You can click the three ... click copy text, and fill it in.
Read the messages under the snippet of code
so like i*=-1
??
No, like what I described here. But I have to go now, sorry.
I can try to help for a bit, could you show where you are currently at? I stopped following the thread ^^
ofc!
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
Ah okay, so you need to calculate your individual terms then
yes so
i think in if statement
i have to do something like this
so do i just make a term and set it equal to -1 under if statement
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.
oh ok
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?
theres no values cause its based on what the user inputs
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.
no no i get it
we basically
have to sort make like an equation
with n and x is what i think
Yes exactly
Though you would use i and x.
Remember you want to iterate over all the terms and n just ist the value for your last term (and the limit for i)
ohh yeahh
so will the equation sort of be like
-x^i/i
for even
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.
do we have to write like i! for more accuracy
just in case
?
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
it multiples from 1 to that number right?
oh so like for loop but mulitplicatino
Yes
so in python
the equation (-)x^i/i!
would be like (-x**i)/
wait how do u write factorail in python
?
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||
im so clueless ๐ญ
i have no idea how factorial statement is
Well you already kinda said it
oh wait nvm
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.
ohh
so the statement will be like
(-x**i)/from i import factorial???
OH ALR LOLL
It's too early for you to worry about imports and stuff
so the factorical in python is like factorical()?
No, python does not have a factorial by default
makes sense im suffering with loops already
So you would have to make it yourself
You still need a loop for factorial
remember 5! = 5 * 4 * 3 * 2 (* 1)
ohh
Do you know how functions work? If not dont worry about it.
wait so is f=f*i wrong?
Yes
help whats the mistake ๐ญ
well its part of the solution, but you need to do that step more than once
ohh okay
Also f=f*i doesn't work if f is undefined
And I don't remember if we defined f somewhere
it wasnt
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)
Ah yeah, you're right
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```
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.
No, you will need a 2nd for loop for your factorial.
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.
oh so like for j in range(1,i+1):
f=f*j
Yes
and then under the for j loop itself we put the equation right?
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
correct
wait when u mean by 2 meaning like instead of starting at one, it goes like 2 3 4 right?
yeah
ohh
Also you need to "reset" f before your inner loop, the best place would be right before your 2nd for loop
for i in range(1,n+1):
if i%2==0:
for j in range(1,i+1):
f=f*j
(-x**i)/f```
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.
wait how do u reset?
like do we write f=1 after the print the equation
?
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
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)
but if we do like that then wont it be hard to do for the odd digits?
or is that still possible?
ohh wait
i got it
i understood
That will actually make it easier for the odd digits, since you don't need the code to calcualte the factorial again
true
so basically we first do the caluclate for the factrical
and then do the equation
calcualte
I can't spell this word for the life of me...
dw my spelling is even worse ๐ญ
Yes, factorial first and then the equation.
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)```
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.
Looks good so far, almost there
so after this what do we do?
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
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)
so like result=1?
uh no i think ๐ญ
i think its one cause in the loop it goes from one to n+1
so i thought result would also be one?
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
oh yeah true
result here is essentialy S in your original screenshot
Out of it, you wouldn't want to reset your result while calculating the series
alr ๐ซก
so in the for i loop
do we just write result= even + odd?
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
So you have 2 options (give me a minute)
ofc!
# 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)
wait so i have a doubt
can we use elif instead of else
cause im very confused on what situations do we use elif and else
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.
how is elif and if different than?
ohh yeah true
else will execute regardless of your condition (but not if one of your previous if or elif statements executed)
You can imagine them like this
if condition:
...
elif condition2:
...
# is equal to
if condition:
...
else:
if condition2:
...
so cant we just write another if statement instead of an elif
for another condition
?
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.
ohh
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.
ohh so like dependent on the if statmenet
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
help yeah loll ๐ญ
OHH
that makes sm sense now
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.
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```
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.
Ah almost!
ohh thank u sm elif always confused me
wait help what is left now ๐ญ
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
ohhh
And the same thing (indentation) happens with your result (the term is only added to your result if your number is odd)
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```
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.
is it correct now?
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```
help im so awful at indenation
that thing has been my biggest hater since i started python
Yeah sorry, can't help you there - that's something you have to learn when writing python
It replaces {} in most other languages
x=float(input("Enter value of x: "))
n=int(input("Enter value of N: "))
term=0
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)```
yeah i have been doing more problems, got way better compared to before ๐
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
ohhhh
Also I removed the term=0 since it's not necessary here
OH IT HAS TO BE INSIDE THE FOR I LOOP ๐ญ
oh yeahhh
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?
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.
Yes, you want to save every term, not just the last one ๐
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 += xi / 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)}")
: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
ohh true true
Good job ๐
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 ๐ญ
You're welcome ๐
i hope u have a wonderful day!!!!
@serene mauve thank u for ur help too!!!
thanks i was so confused loll
!close
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.