[英]while loop not printing after passing user input
我刚开始使用python。 我在调用函数时遇到问题。 当我传入用户输入时,它不会打印出来。 如果我在 loopit() 中定义一个数字,该函数将打印出来。
程序:
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("> "))
输入:2
所需的输出:
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
实际输出:
####-Air:Lpthw #####$ python ex33.py
Please input a number
> 2
#####-Air:Lpthw ####$
所以在将用户输入插入循环后,我没有得到我正在寻找的打印输出。 但是,当循环没有参数且内部有定义的变量时,输出会正确打印出来。
我不知道如何解决这个问题,我可能遗漏了一些东西,请帮忙:)。
谢谢一堆!
在您的def loopit(i):
,您将i
用作int
。
但是loopit(raw_input("> "))
将 input 转换为string
。 你应该做转换。
尝试这个
loopit(int(raw_input("> ")))
或者
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.