Python比较与大整数不正确

时间:2014-09-05 05:36:01

标签: python math comparison long-integer

刚刚开始学习一点python,由于某种原因,我刚刚放在一起的脚本返回一个数字,如10000000000000000< 5.我认为这是由于int类型的本地不准确值,但是我不确定,也许我只是做错了!

这是我的(写得不好,我知道)剧本:

def checkValue(n):
    while True:
        if n == '':
            print 'You didn\'t enter anything!'
            return False
        else:
            try:
                n = int(n)
            except ValueError:
                print 'That is not an integer!'
                return False
            else:
                break

    return True

while True:
    firstNum = raw_input('Enter the first number: ')
    if checkValue(firstNum) == False:
        continue
    else:
        break
while True:
    secNum = raw_input('Enter the second number: ')
    if checkValue(secNum) == False:
        continue
    else:
        break
while True:
    thirdNum = raw_input('Enter the third number: ')
    if checkValue(thirdNum) == False:
        continue
    else:
        break

if thirdNum > secNum and thirdNum > firstNum:
    print 'The third number is the biggest.'
elif secNum > firstNum:
    print 'The second number is the biggest.'
else:
    print 'The first number is the biggest.'

2 个答案:

答案 0 :(得分:2)

在“checkValue”功能中,您将输入转换为“int”。但在比较中,您使用输入的字符串值。因此,您可以在输入阶段转换“firstNum”,“secNum”和“thirdNum”。看看差异。

In [2]: firstNum = raw_input('Enter the first number: ')

In [3]: firstNum
Out[3]: '5'

In [4]: int_first = int(firstNum)

In [5]: int_first
Out[5]: 5

答案 1 :(得分:1)

您需要将原始输入firstNum等转换为intfirstNum = int(firstNum)的整数。