943,645 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1076
  • Python RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Dec 17th, 2008
0

Re: Beginners Problem

ok this is what i have;
from math import *

x=input("What value will you give x?:")
n=input("Please set a maximium amount of iterations?:")
for i in range (n):
x = x - ((x-cos(x))/(1 + sin(x)))
break
y =x
if (y-x) <= 10**-8:
print y
elif (y-x) > 10**-8:
continue

still not working..advice??
Please use code tags when posting code in this forum. To use code tags wrap your code in these guys:
[code=python]
# Code in here
[/code]

Just to be nice I'll wrap your code for you:
python Syntax (Toggle Plain Text)
  1. from math import *
  2.  
  3. x=input("What value will you give x?:")
  4. n=input("Please set a maximium amount of iterations?:")
  5. for i in range (n):
  6. x = x - ((x-cos(x))/(1 + sin(x)))
  7. break
  8. y =x
  9. if (y-x) <= 10**-8:
  10. print y
  11. elif (y-x) > 10**-8:
  12. continue
See how much nicer that looks? And indentation is not lost!

So first thing: remove the break and the continue statements.
Next, y = x should be assigned at the end of your loop.

That should get you a little closer
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Jan 6th, 2009
0

Re: Beginners Problem

Python Syntax (Toggle Plain Text)
  1. x = initial_value
  2. x_last = x
  3.  
  4. threshold = minimum_change
  5. MAXITER = maximum_iterations_desired
  6. iterations = 0
  7.  
  8. while iterations < MAXITER and x - x_last > threshold:
  9. x_last = x
  10. x = iterate(x)
  11.  
  12. # x now holds your result

Using break and continue is overcomplicating things, IMO.
Last edited by Nuez_Jr; Jan 6th, 2009 at 3:15 pm. Reason: Apparently [icode] doesn't have a language parameter ; )
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Nuez_Jr is offline Offline
18 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Avoiding multiple openings of the same file in python
Next Thread in Python Forum Timeline: urllib - check if web page exists





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC