b4and5 发表于 2014-05-13 16:08

while-loop

本帖最后由 b4and5 于 2014-05-13 16:09 编辑

小弟最近在学习python,遇到个问题.....请看:
cat exc33.py#limit = raw_input("Input limit:")
limit = 6
i = 0

numbers = []

while i < limit:
    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
结果显而易见,也不会掉进无限循环里头。
但是,当我把limit=6注释掉,改用 limit = raw_input("Input limit:")的时候,
>python exc33.py
>Input limit:
当我输入6的时候,程序掉进了无限的循环里....
这是为什么呢?.....请好心人来帮帮小弟吧....:cry:

reyleon 发表于 2014-05-13 17:18

因为你输入的东东,哪怕你输入的是数字,它也是字符串类型,不是数字类型.! so ...

while i < int(limit):

b4and5 发表于 2014-05-13 17:38

回复 2# reyleon

谢谢!刚刚好看到int这个函数...哈哈哈


   

elu_ligao 发表于 2014-05-18 16:10

为什么 int < str ?

我的意思是 例如这样:

    >>> n = '0'
>>> 1 < n
True
>>> 11 < n
True
>>> 111 < n
True
>>> '1' < n
False

icymirror 发表于 2014-05-19 10:10

回复 4# elu_ligao
官方文档的说明,
CPython implementation detail: Objects of different types except numbers are ordered by their type names; objects of the same types that don’t support proper comparison are ordered by their address.
也就是说:不管int的值是多少,在和字符串比较的时候,是比较的类型的名字,不是具体的值。
页: [1]
查看完整版本: while-loop