Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2012, 07:11 PM
rms730 rms730 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 rms730 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 23 m 15 sec
Reputation Power: 0
While loops

python novice.
Trying to link two functions with embedded while loops. Each function (base(i) and sqform(base) works on its own. But when linked I can't get it to work.

Attempting to get the following output:

ex: i = 4

4.0, 2.0, 2.0
3.0, 1.73, 1.73
2.0, 1.41, 1.41
1.0, 1.0, 1.0

currently only getting:

4.0, 2.0, 2.0

using version 2.7.3 writing gedit

Code:
from math import *

def base(i):
    base = i
    while base > 0:
        sqform(base)
        base = base - 1

def sqform(base):
    a = base
    x = base - 1
    y = (x + a/x)/2
    while y != x:
        x = y
        y = (x + a/x)/2
    else:
        print float(a), float(x), float(sqrt(a))

i = int(raw_input('Enter number.\n'))
base(i)

Reply With Quote
  #2  
Old November 5th, 2012, 09:35 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
Hint: what's the result of "3/2" in Python (before Python 3)? It isn't 1.5. You can avoid this either by making sure the arguments to sqform are always float or by putting the line
Code:
from __future__ import division
at the top of your code. (Or just switch to Python 3, but that will break other parts of your code.)

Another important thing: it's almost never a good idea to use exact equality of iterates as your convergence criterion in an iterative computation of floating-point values. In the case of your algorithm, there are cases where the iteration will enter an infinite loop. (If you start trying randomly generated inputs, you'll hit one eventually.) The usual way to decide when the iteration has "converged" is to see if the difference of two consecutive iterates is small enough. In other words, replace this:
Code:
    while y != x:
with this:
Code:
    while abs(y-x) > tolerance:
`tolerance' should be set to some small number of your choosing. For example, if tolerance=1e-14, then you'll get about 14 digits of accuracy, which is probably more than you'll need.

Reply With Quote
  #3  
Old November 5th, 2012, 10:41 PM
rms730 rms730 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 7 rms730 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 23 m 15 sec
Reputation Power: 0
Thanks - very helpful

Reply With Quote
  #4  
Old November 6th, 2012, 09:55 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
Questionable use of while: else:

Since your while loop hasn't got a break statement the code in the else: block always executes after the while condition fails. Your code would be less confusing without the else
Code:
while condition:
    # statements
statement
It's likely, and understandable, that an earlier version of your program had a break statement and you didn't bother to change the else since it's valid.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > While loops

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap