While loop over numbers doesn't seem to be working [duplicate]
This question already has an answer here:
Reading in integer from stdin in Python
4 answers
I am new to python and I am trying run this piece of code, however, the while loop doesn't seem to be working. Any ideas?
def whilelooper(loop):
i = 0
numbers =
while i < loop:
print "At the top i is %d" %i
numbers.append(i)
i += 1
print "numbers now:",numbers
print "At the bottom i is %d" %i
print "the numbers:",
for num in numbers:
print num
print "Enter a number for loop"
b = raw_input(">")
whilelooper(b)
python python-2.x
marked as duplicate by cricket_007, tripleee, Community♦ Nov 16 '18 at 9:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 4 more comments
This question already has an answer here:
Reading in integer from stdin in Python
4 answers
I am new to python and I am trying run this piece of code, however, the while loop doesn't seem to be working. Any ideas?
def whilelooper(loop):
i = 0
numbers =
while i < loop:
print "At the top i is %d" %i
numbers.append(i)
i += 1
print "numbers now:",numbers
print "At the bottom i is %d" %i
print "the numbers:",
for num in numbers:
print num
print "Enter a number for loop"
b = raw_input(">")
whilelooper(b)
python python-2.x
marked as duplicate by cricket_007, tripleee, Community♦ Nov 16 '18 at 9:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
What do you hope for the code to do and what actual behavior do you observe? Also, please edit the code to fix the indentation.
– tripleee
Nov 16 '18 at 9:41
2
raw_input
returns a string... you needwhilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
1
By the way,for i in range(loop)
would be "safer" than a while loop
– cricket_007
Nov 16 '18 at 9:45
1
@PriyankaYadav that was me but you're welcome :D
– Ben Jones
Nov 16 '18 at 9:53
1
You may use the checkmark next to the answer below to accept it
– cricket_007
Nov 16 '18 at 9:53
|
show 4 more comments
This question already has an answer here:
Reading in integer from stdin in Python
4 answers
I am new to python and I am trying run this piece of code, however, the while loop doesn't seem to be working. Any ideas?
def whilelooper(loop):
i = 0
numbers =
while i < loop:
print "At the top i is %d" %i
numbers.append(i)
i += 1
print "numbers now:",numbers
print "At the bottom i is %d" %i
print "the numbers:",
for num in numbers:
print num
print "Enter a number for loop"
b = raw_input(">")
whilelooper(b)
python python-2.x
This question already has an answer here:
Reading in integer from stdin in Python
4 answers
I am new to python and I am trying run this piece of code, however, the while loop doesn't seem to be working. Any ideas?
def whilelooper(loop):
i = 0
numbers =
while i < loop:
print "At the top i is %d" %i
numbers.append(i)
i += 1
print "numbers now:",numbers
print "At the bottom i is %d" %i
print "the numbers:",
for num in numbers:
print num
print "Enter a number for loop"
b = raw_input(">")
whilelooper(b)
This question already has an answer here:
Reading in integer from stdin in Python
4 answers
python python-2.x
python python-2.x
edited Nov 16 '18 at 9:43
cricket_007
83.8k1147118
83.8k1147118
asked Nov 16 '18 at 9:35
Priyanka YadavPriyanka Yadav
132
132
marked as duplicate by cricket_007, tripleee, Community♦ Nov 16 '18 at 9:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by cricket_007, tripleee, Community♦ Nov 16 '18 at 9:52
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
What do you hope for the code to do and what actual behavior do you observe? Also, please edit the code to fix the indentation.
– tripleee
Nov 16 '18 at 9:41
2
raw_input
returns a string... you needwhilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
1
By the way,for i in range(loop)
would be "safer" than a while loop
– cricket_007
Nov 16 '18 at 9:45
1
@PriyankaYadav that was me but you're welcome :D
– Ben Jones
Nov 16 '18 at 9:53
1
You may use the checkmark next to the answer below to accept it
– cricket_007
Nov 16 '18 at 9:53
|
show 4 more comments
1
What do you hope for the code to do and what actual behavior do you observe? Also, please edit the code to fix the indentation.
– tripleee
Nov 16 '18 at 9:41
2
raw_input
returns a string... you needwhilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
1
By the way,for i in range(loop)
would be "safer" than a while loop
– cricket_007
Nov 16 '18 at 9:45
1
@PriyankaYadav that was me but you're welcome :D
– Ben Jones
Nov 16 '18 at 9:53
1
You may use the checkmark next to the answer below to accept it
– cricket_007
Nov 16 '18 at 9:53
1
1
What do you hope for the code to do and what actual behavior do you observe? Also, please edit the code to fix the indentation.
– tripleee
Nov 16 '18 at 9:41
What do you hope for the code to do and what actual behavior do you observe? Also, please edit the code to fix the indentation.
– tripleee
Nov 16 '18 at 9:41
2
2
raw_input
returns a string... you need whilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
raw_input
returns a string... you need whilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
1
1
By the way,
for i in range(loop)
would be "safer" than a while loop– cricket_007
Nov 16 '18 at 9:45
By the way,
for i in range(loop)
would be "safer" than a while loop– cricket_007
Nov 16 '18 at 9:45
1
1
@PriyankaYadav that was me but you're welcome :D
– Ben Jones
Nov 16 '18 at 9:53
@PriyankaYadav that was me but you're welcome :D
– Ben Jones
Nov 16 '18 at 9:53
1
1
You may use the checkmark next to the answer below to accept it
– cricket_007
Nov 16 '18 at 9:53
You may use the checkmark next to the answer below to accept it
– cricket_007
Nov 16 '18 at 9:53
|
show 4 more comments
1 Answer
1
active
oldest
votes
Your input is inputted as a string type, but the comparator
while i < loop:
is expecting both i and loop to be of type int (for integer), in order to be able to compare them.
You can fix this by casting loop to an int:
def whilelooper(loop):
i = 0
numbers =
loop = int(loop)
...
1
Orwhilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
Better to do it inside the function, because it's always going to want to be an int for the function to work
– Ben Jones
Nov 16 '18 at 9:47
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your input is inputted as a string type, but the comparator
while i < loop:
is expecting both i and loop to be of type int (for integer), in order to be able to compare them.
You can fix this by casting loop to an int:
def whilelooper(loop):
i = 0
numbers =
loop = int(loop)
...
1
Orwhilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
Better to do it inside the function, because it's always going to want to be an int for the function to work
– Ben Jones
Nov 16 '18 at 9:47
add a comment |
Your input is inputted as a string type, but the comparator
while i < loop:
is expecting both i and loop to be of type int (for integer), in order to be able to compare them.
You can fix this by casting loop to an int:
def whilelooper(loop):
i = 0
numbers =
loop = int(loop)
...
1
Orwhilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
Better to do it inside the function, because it's always going to want to be an int for the function to work
– Ben Jones
Nov 16 '18 at 9:47
add a comment |
Your input is inputted as a string type, but the comparator
while i < loop:
is expecting both i and loop to be of type int (for integer), in order to be able to compare them.
You can fix this by casting loop to an int:
def whilelooper(loop):
i = 0
numbers =
loop = int(loop)
...
Your input is inputted as a string type, but the comparator
while i < loop:
is expecting both i and loop to be of type int (for integer), in order to be able to compare them.
You can fix this by casting loop to an int:
def whilelooper(loop):
i = 0
numbers =
loop = int(loop)
...
answered Nov 16 '18 at 9:41
Ben JonesBen Jones
15113
15113
1
Orwhilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
Better to do it inside the function, because it's always going to want to be an int for the function to work
– Ben Jones
Nov 16 '18 at 9:47
add a comment |
1
Orwhilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
Better to do it inside the function, because it's always going to want to be an int for the function to work
– Ben Jones
Nov 16 '18 at 9:47
1
1
Or
whilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
Or
whilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
Better to do it inside the function, because it's always going to want to be an int for the function to work
– Ben Jones
Nov 16 '18 at 9:47
Better to do it inside the function, because it's always going to want to be an int for the function to work
– Ben Jones
Nov 16 '18 at 9:47
add a comment |
1
What do you hope for the code to do and what actual behavior do you observe? Also, please edit the code to fix the indentation.
– tripleee
Nov 16 '18 at 9:41
2
raw_input
returns a string... you needwhilelooper(int(b))
– cricket_007
Nov 16 '18 at 9:44
1
By the way,
for i in range(loop)
would be "safer" than a while loop– cricket_007
Nov 16 '18 at 9:45
1
@PriyankaYadav that was me but you're welcome :D
– Ben Jones
Nov 16 '18 at 9:53
1
You may use the checkmark next to the answer below to accept it
– cricket_007
Nov 16 '18 at 9:53