问题
I'm new to python and I am trying to make a code to print all the square numbers until the square of the desired value entered by the user.
n = raw_input("Enter number")
a = 1
while a < n:
a = 1
print(a*a)
a += 1
if a > n:
break
When I run this code it infinitely prints "1" ... I'm guessing that the value of a
does not increase by +=
so it's a=1
forever. How do I fix this?
回答1:
There are some problems. First, your input (what raw_input()
returns) is a string, so you must convert it to integer:
n = int(raw_input(...))
Second, you are setting a = 1
each iteration, so, since the loop condition is a < n
, the loop will run forever ( if n > 1
). You should delete the line
a = 1
Finally, it's not necesary to check if a > n
, because the loop condition will handle it:
while a < n:
print a * a
a += 1
# 'if' is not necessary
回答2:
There is a small error in your code:
while a < n:
a=1 # `a` is always 1 :)
print a*a
a += 1
if a > n:
break
回答3:
You're setting the value of a
back to 1
on every iteration of the loop, so every time it checks against n
, the value is 2
. Remove the a=1
line.
回答4:
As others have noted, your specific problem is resetting a
each time you loop. A much more Pythonic approach to this is the for
loop:
for a in range(1, n):
print(a ** 2)
This means you don't have to manually increment a
or decide when to break
or otherwise exit a while
loop, and is generally less prone to mistakes like resetting a
.
Also, note that raw_input
returns a string, you need to make it into an int
:
n = int(raw_input("Enter number: "))
回答5:
an even better idea is to make a simple function
def do_square(x):
return x*x
then just run a list comprehension on it
n = int(raw_input("Enter #:")) #this is your problem with the original code
#notice we made it into an integer
squares = [do_square(i) for i in range(1,n+1)]
this is a more pythonic way to do what you are trying to do you really want to use functions to define functional blocks that are easy to digest and potentially can be reused
you can extend this concept and create a function to get input from the user and do some validation on it
def get_user_int():
#a function to make sure the user enters an integer > 0
while True:
try:
n = int(raw_input("Enter a number greater than zero:"))
except TypeError:
print "Error!! expecting a number!"
continue;
if n > 0:
return n
print "Error: Expecting a number greater than zero!"
and then you can build your input right into your list
squares = [do_square(i) for i in range(1,get_user_int()+1)]
and really do_square
is such a simple function we could easily just do it in our loop
squares = [x*x for x in range(1,get_user_int())]
回答6:
The first line in your loop sets's a
to one on every iteration.
回答7:
You assign a=1 inside the loop. That means it's overwriting the a+=1
.
回答8:
try this:
n = eval(raw_input("Enter number"))
a=1
while a < n:
print a*a
a += 1
The issue here is that the value of a gets overridden every time you enter in the loop
回答9:
Problem is in the condition of the while loop, to print squares of numbers upto a limiting value, try this..
def powers(x):
n=1
while((n**2)<=x):
print(n**2, end =' ')
n +=1
来源:https://stackoverflow.com/questions/22304562/square-number-sequence-in-python