简体   繁体   English

pyschools 平方根近似

[英]pyschools square root approximation

I am new to Python and stackoverflow.我是 Python 和 stackoverflow 的新手。 I have been trying to solve Pyschools 'while loop' example for square root approximation (Topic 5: Question 9).我一直在尝试解决 Pyschools 'while loop' 平方根近似的示例(主题 5:问题 9)。 However I am unable to get the desired output.但是我无法获得所需的输出。 I am not sure if this issue is related to the loop or the formula.我不确定这个问题是否与循环或公式有关。 Here is the question:这是问题:

Create a function that takes in a positive number and return 2 integers such that the number is between the squares of the 2 integers.创建一个函数,该函数接受一个正数并返回 2 个整数,使得该数字介于 2 个整数的平方之间。 It returns the same integer twice if the number is a square of an integer.如果数字是整数的平方,它会返回相同的整数两次。


Examples:例子:

sqApprox(2)
(1, 2)
sqApprox(4)
(2, 2)
sqApprox(5.1)
(2, 3)

Here is my code:这是我的代码:


def sqApprox(num):
    i = 0
    minsq = 1                           # set lower bound
    maxsq = minsq                       # set upper bound
    while i*i<=num:                     # set 'while' termination condition
        if i*i<=num and i >=minsq:  # complete inequality condition  
            minsq = i
        if i*i<=num and i <=maxsq:  # complete inequality condition
            maxsq = i
        i=i+1                       # update i so that 'while' will terminate
    return (minsq, maxsq)

If I create this function sqApprox(4) and call it on IDE, I get output (2, 0) .如果我创建这个函数sqApprox(4)并在 IDE 上调用它,我会得到输出(2, 0)

Could someone please let me know what I am doing wrong?有人可以让我知道我做错了什么吗? Thanks in advance.提前致谢。

This is why your code does what it does:这就是为什么你的代码做它所做的:

After the line maxsq = minsq is executed, both of those values are 1.在执行maxsq = minsq行之后,这两个值都是 1。

When we come to the loop当我们来到循环

while i*i<=num:                     # set 'while' termination condition
    if i*i<=num and i >=minsq:  # complete inequality condition  
        minsq = i
    if i*i<=num and i <=maxsq:  # complete inequality condition
        maxsq = i
    i=i+1                       # update i so that 'while' will terminate

first note that inside the loop i*i<=num , so there is no need to retest it.首先请注意,在循环i*i<=num ,因此无需重新测试它。 Thus it is equivalent to:因此它等价于:

 while i*i<=num: 
     if i >=minsq:  
         minsq = i
     if i <=maxsq:
         maxsq = i
     i=i+1

In the first pass through the loop i == 0 but maxsq == 1 , making the second condition true, hence setting maxsq equal to the current value of i , which is 0. In subsequent passes through the loop, i <= maxsq is false (since maxsq == 0 but i > 0 ) hence maxsq is never moved beyond 0. On the other hand, the first condition in the while loop keeps updating minsq as intended.在第一次通过循环i == 0maxsq == 1 ,使第二个条件为真,因此设置maxsq等于i的当前值, i 0。在随后的循环中, i <= maxsq是false (因为maxsq == 0i > 0 )因此maxsq永远不会超过 0。另一方面,while 循环中的第一个条件会按预期不断更新minsq

I would recommend forgetting about both minsq and maxsq completely.我建议完全忘记minsqmaxsq Have the loop simply be:让循环简单地是:

while i*i <= num:
    i += 1 #shortcut for i = i + 1

When the loop is done executing, a simple test involving i-1 is enough to determine what to return.当循环执行完毕后,一个涉及i-1的简单测试就足以确定返回什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM