[英]while loop not printing after passing user input
I just started out with python.我刚开始使用python。 I have a problem when I am calling out my function.我在调用函数时遇到问题。 Its not printing out when I pass in the user input.当我传入用户输入时,它不会打印出来。 if I define a number inside of loopit() the function will print out.如果我在 loopit() 中定义一个数字,该函数将打印出来。
Program:程序:
def loopit(i):
numbers=[]
while i < 6:
print "At the top i is %d" % i
numbers.append(i)
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 "Please input a number"
loopit(raw_input("> "))
Input: 2输入:2
Desired output :所需的输出:
At the top i is 2
Numbers now: [2]
At the bottom i is 3
The numbers :
At the top i is 3
Numbers now: [2, 3]
At the bottom i is 4
The numbers :
At the top i is 4
Numbers now: [2, 3, 4]
At the bottom i is 5
The numbers :
At the top i is 5
Numbers now: [2, 3, 4, 5]
At the bottom i is 6
The numbers :
2
3
4
5
Actual output:实际输出:
####-Air:Lpthw #####$ python ex33.py
Please input a number
> 2
#####-Air:Lpthw ####$
So I am not getting the printed output that I was looking for after inserting the user input into the loop.所以在将用户输入插入循环后,我没有得到我正在寻找的打印输出。 However,when the loop has no arguments and a defined variable inside it, the output gets printed out correctly.但是,当循环没有参数且内部有定义的变量时,输出会正确打印出来。
I don't know how to work around this, I am probably missing something, please help :).我不知道如何解决这个问题,我可能遗漏了一些东西,请帮忙:)。
Thanks a bunch !谢谢一堆!
In your def loopit(i):
, you are using i
as a int
.在您的def loopit(i):
,您将i
用作int
。
But the loopit(raw_input("> "))
converts input to string
.但是loopit(raw_input("> "))
将 input 转换为string
。 You should do the convert.你应该做转换。
Try this尝试这个
loopit(int(raw_input("> ")))
OR或者
def loopit(i):
numbers=[]
i = int(i)
while i < 6:
print "At the top i is %d" % i
.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.