Python Forum
Sum Series w/ Fractions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Sum Series w/ Fractions (/thread-416.html)

Pages: 1 2


Sum Series w/ Fractions - EwH006 - Oct-10-2016

Good Morning,

I am new to programming and these forums. I plan on becoming a little more active here and on IRC while working toward my degree.

This morning I'm trying to work toward an assignment I have yet to start/complete. I did class work on the subject last Friday so I have an idea how loops work.

The homework I'm doing is just like the class work, except it uses fractions instead to sum. When I change the loop condition to a fraction number I get the floating point error because I believe the loop wants integers.

If someone could give me an idea where to inject fractions or point to a reference where I could find out more I'd appreciate it. Thanks

This is a sample of the class work loop (PYTHON 3): 
sum = 0

for count in range(0, 10, 1):
  print((count+1), end=" ")

print(" = ", sum)



RE: Sum Series w/ Fractions - ichabod801 - Oct-10-2016

First, note that the example given as class work never sums anything. It's going to print the sum from 1 to 10 as 0, when it's actually 55.

Second, the problem as you state it is not clear. What fractions are you supposed to be summing?

Third, try something and tell us clearly how it doesn't work. We don't like to write code for people, we like to help people fix code that isn't working.


RE: Sum Series w/ Fractions - EwH006 - Oct-10-2016

I understand what you mean by: "I'm not summing anything" , I believe. I'm trying to figure out how I replace the code I originally posted which adds "1  +  2  +  3  +  4  +  5  +  6  +  7  +  8  +  9  +  10  +   =  55"

Instead I need to do this with fractions. How would I go about, or where would I go about looking to find information on this. I know your not gonna write code for me thats understood.


RE: Sum Series w/ Fractions - ichabod801 - Oct-10-2016

(Oct-10-2016, 05:14 PM)EwH006 Wrote: I'm trying to figure out how I replace the code I originally posted which adds "1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + = 55"

No, it doesn't. Run the code you posted and see what the output is.


RE: Sum Series w/ Fractions - EwH006 - Oct-10-2016

(Oct-10-2016, 05:23 PM)ichabod801 Wrote:
(Oct-10-2016, 05:14 PM)EwH006 Wrote: I'm trying to figure out how I replace the code I originally posted which adds "1  +  2  +  3  +  4  +  5  +  6  +  7  +  8  +  9  +  10  +   =  55"

No, it doesn't. Run the code you posted and see what the output is.

Your making something out of nothing. Your being case sensitive about my damn question.


sum = 0

for count in range(0, 10, 1): #starting point, ending, loop update condition (alternate to figure out)
   sum = sum + (count+1)
   print((count+1), " + ", end = " ")

print(" = ", sum)

print("\n Add a series using WHILE loop")



RE: Sum Series w/ Fractions - ichabod801 - Oct-10-2016

Good. You fixed the first problem I mentioned in my first response. If you fix the other two, I'll be happy to help you.


RE: Sum Series w/ Fractions - Mekire - Oct-10-2016

Well, either way the current "way" you have shown is terrible.
sum is a builtin; you can use it directly.

>>> print(sum(range(11)))

55
This method will have no issue with fractions:
>>> from fractions import Fraction
>>>
>>> numbers = [Fraction(2/3), Fraction(7,5), Fraction(4,9)]
>>> sum(numbers)
Fraction(83, 45)
>>>



RE: Sum Series w/ Fractions - EwH006 - Oct-10-2016

(Oct-10-2016, 06:07 PM)EwH006 Wrote:
(Oct-10-2016, 05:23 PM)ichabod801 Wrote:
(Oct-10-2016, 05:14 PM)EwH006 Wrote: I'm trying to figure out how I replace the code I originally posted which adds "1  +  2  +  3  +  4  +  5  +  6  +  7  +  8  +  9  +  10  +   =  55"

No, it doesn't. Run the code you posted and see what the output is.

Your making something out of nothing. Your being case sensitive about my damn question.


sum = 0

for count in range(0, 10, 1): #starting point, ending, loop update condition (alternate to figure out)
   sum = sum + (count+1)
   print((count+1), " + ", end = " ")

print(" = ", sum)

print("\n Add a series using WHILE loop")

It was a typo to begin with, pfft. Mekire


RE: Sum Series w/ Fractions - Mekire - Oct-10-2016

(Oct-10-2016, 06:23 PM)EwH006 Wrote: It was a typo to begin with, pfft. Mekire

I wasn't referring to any typo.  Your corrected, working code, that sums the range was what I was referring to.  Anyway, what do you mean by sum fractions?  Without a better explanation we can't help.  You want a range that takes non integer steps?


RE: Sum Series w/ Fractions - EwH006 - Oct-10-2016

(Oct-10-2016, 06:27 PM)Mekire Wrote:
(Oct-10-2016, 06:23 PM)EwH006 Wrote: It was a typo to begin with, pfft. Mekire

I wasn't referring to any typo.  Your corrected, working code, that sums the range was what I was referring to.  Anyway, what do you mean by sum fractions?  Without a better explanation we can't help.  You want a range that takes non integer steps?

Sorry had to go to class. I was thanking you Mekire for the post you had previously submitted. I can't edit / delete my posts. Anyway I'm looking to do the same thing with the code I gave but I need to use the following fractions, in which I don't know how apply to the syntax of while or for loops: 1/3 , 3/5, 5/7, 7/9, 9/11, 11/13, +..... all the way to 97/99 = 45.12445030305

It won't let me use floating point numbers in the for loop for sure.