Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Looping Trouble
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Comp.Sci




PostPosted: Thu Oct 13, 2011 5:36 pm   Post subject: Looping Trouble

Here is the question:

The Series 1/1 + 1/2 + 1/3 + 1/4 + ? is a divergent series. Create a program that will help us examine this claim. Your program will have the user enter the number of terms from the series and tell them the sum to three decimals. An example of a run:
Enter the number of terms:4
2.083
----------------------------------------------------
I'm not too worried about the three decimal place total, it's the looping that concerns me... I have done this on paper and made a table... everything seems fine.
The problem is that i keep getting 2 as my total, for any term I input.

term=input("Enter the number of terms:")
total=1
div=0 #Counter
x=1 #Numerator
y=0 #Denominator
total=1
while div<=term:
div=div+1
y=y+1
x=x/y
total=total+x
print total #I have tried indenting "print total" into the loop and see what total i would get for each loop, and i still end up get 2.. PLEASE HELP
Sponsor
Sponsor
Sponsor
sponsor
Comp.Sci




PostPosted: Thu Oct 13, 2011 5:38 pm   Post subject: RE:Looping Trouble

keep in mind that when the user inputs 3 terms, the program will select 1/1, 1/2, and 1/3...
Tony




PostPosted: Thu Oct 13, 2011 6:05 pm   Post subject: Re: RE:Looping Trouble

Comp.Sci @ Thu Oct 13, 2011 5:38 pm wrote:
when the user inputs 3 terms, the program will select 1/1, 1/2, and 1/3...

Will your program actually use those values?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Comp.Sci




PostPosted: Thu Oct 13, 2011 7:07 pm   Post subject: RE:Looping Trouble

Yes, there are an infinite number of terms.. If the user enters 10 terms, it would look like 1/1+1/2+1/3+1/4+1/5+1/6+1/7+1/8+1/9+1/10...
I have to add all of these terms and get a total... for some reason, i only get 2 as my sum
Raknarg




PostPosted: Thu Oct 13, 2011 7:30 pm   Post subject: RE:Looping Trouble

y=y+1
x=x/y
total=total+x

You are changing the value of x in every loop, that may be the problem. Try this:

total = total + (x / y)

Also, why are you starting total as 1?
Tony




PostPosted: Thu Oct 13, 2011 7:43 pm   Post subject: Re: RE:Looping Trouble

Comp.Sci @ Thu Oct 13, 2011 7:07 pm wrote:
Yes, there are an infinite number of terms.. If the user enters 10 terms, it would look like 1/1+1/2+1/3+1/4+1/5+1/6+1/7+1/8+1/9+1/10...
I have to add all of these terms and get a total... for some reason, i only get 2 as my sum

The "some reason" is because it does not look like "1/1+1/2+1/3+1/4+1/5+1/6+1/7+1/8+1/9+1/10".
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: