Python对大整数的比较不正确

2024-09-29 23:24:18 发布

您现在位置:Python中文网/ 问答频道 /正文

刚刚开始学习一些python,出于某种原因,我编写的脚本返回一个数字,比如100000000000000<;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.'

Tags: thefalsetruenumberreturnifiselse
2条回答

{{you正在转换“{cd2>中的输入}”。但是在比较时使用输入的字符串值。所以可以在输入阶段转换“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

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

相关问题 更多 >

    热门问题