Python Forum
Sum Series w/ Fractions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sum Series w/ Fractions
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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")
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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)
>>>
Reply
#8
(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
Reply
#9
(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?
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Periodic Fractions with Fraction or float_to_ratio? TimeMen 3 2,971 May-20-2018, 03:48 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

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