質問

I'm stuck on a Python 101 type problem involving loops. Here are the directions:

The square numbers are the integers of the form K × K, e.g. 9 is a square number since 3 × 3 = 9. Write a program that reads an integer n from input and outputs all the positive square numbers less than n, one per line in increasing order. For example, if the input is 16, then the correct output would be

1
4
9

This is what I have so far but it sort of works but runs on forever. My code never reaches the if statement so it breaks(stops) before it gets to 17.

Suppose n = 17.

n=int(input())
counter = 1
while counter * counter < n:
   for counter in range(1,n):
      a = counter*counter
      print(a)
   if a < n:
      break

Results:

1
4
9
16
25
36
49
64
81
役に立ちましたか?

解決

Here is a correction of your code.

n=int(input())
counter = 1
for counter in range(1,n):
    a = counter*counter
    if a >= n:
        break
    print(a)

There were three things wrong with your code. First, the condition you want to break on is a >= n not a < n. Second, that condition needs to be tested before you print the number. Thus the if statement needs to be inside the for loop and before your print, statement. Third, the outer while loop is not really necessary :) Though you can add it, but a simple inner for loop will suffice.

他のヒント

if a < n: will never succeed unless n = 2; because inside the loop a is becoming (n-1)*(n-1) which is greater than n for n > 2; that's why the infinite loop. Try this:

>>> counter = 1
>>> n = 16 # int(input())
>>> r = counter**2
>>> while r<n:
    print r
    counter += 1
    r = counter**2


1
4
9

Or just modify yours one by removing the outer loop, and placing the conditional inside the for loop like:

for counter in range(1,n):
      a = counter*counter
      if a >= n:break
      print(a)

Your code loops might be the case in you semantics error try this out light on the memory and simple

def number(n):

for i in range(0,n):
    w=i*i
    if w>n-1:
        break

    print(w)

number(144)

You've got three issues here, but, as you can tell, you're on the right track.

  1. First off, you're using two loops when you only need to be using one, and I think it's because you're a little unclear as to how the while loop works. The while loop checks that the condition is true before each time it runs. If the condition becomes false while going through the loop, the loop will still finish - it just won't start another. For example:

    n = 17
    while n < 18:
        n += 1
        print n
        n += 1
        print n
    

    prints:

    18
    19
    

    In your case, each iteration through the while loop creates a for loop. In order for a single iteration through the while to take place, your computer has to go through for every number from 1 to n, meaning that it'll print out all those extra numbers before your while loop even has a second chance to do its check. The easiest way to fix this is to remove the while loop and structure your code a little differently. As I'll show you in a few lines, you don't really need it.

  2. When you say if a < n:, you've got your sign backwards and you need an equals sign. The problem asks that you give all values less than n, but, without the =, the program won't stop until it's greater than n. It should be if a >= n:.

  3. Finally, the order of the operations isn't what you want it to be. You'd like it to check that a is less than n before printing, but you print before you do that check. If you switch them around, you'll get something like this:

    n=int(input())
    for counter in range(1,n):
        a = counter*counter
        if a >= n:
            break
        print(a)
    

    which should do the trick.

What about

    n= int(input())
    counter= 1
    while counter * counter < n:
        print( counter * counter )
        counter+= 1

?

Turtle man was perfect but for me. I needed to be able to get all the way to 100 so if you need to get past 81 do this

n = int(input())

counter = 1

for counter in range(1,n + 1):
    
    a = counter*counter
    
    if a > n:
        
break
    
print(a)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top