相同代码在python3和python2之间的运行结果不同 [英] different run results between python3 and python2 for the same code

查看:140
本文介绍了相同代码在python3和python2之间的运行结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在python3中运行此python代码时,它显示的结果与python2不同?为什么会有不同的值?

when i run this python code in python3 it shows different results from python2? why there are different values?

    d = 0
    x1 = 0
    x2 = 1
    y1 = 1
    e=125
    phi=238
    temp_phi = phi

    while e > 0:
        print (e, temp_phi)
        temp1 = temp_phi / e
        print (temp1)
        temp2 = temp_phi - temp1 * e
        print (temp2)
        temp_phi = e
        e = temp2

        x = x2 - temp1 * x1
        y = d - temp1 * y1

        x2 = x1
        x1 = x
        d = y1
        y1 = y
    print (d + phi)
    if temp_phi == 1:
         print (d + phi)

推荐答案

问题出在这一行:

temp1 = temp_phi / e

在Python 2中,当/运算符的两个参数均为整数时,将执行整数除法.也就是说,从概念上讲,它等同于 floor(float(a)/float(b)),其中 a b 整数参数.在Python 3中,/是浮点除法的,而不考虑其参数的类型,并且//运算符会重新创建Python 2中/的行为.

In Python 2, the / operator performs integer division when both its arguments are integers. That is, it is equivalent (conceptually) to floor(float(a) / float(b)), where a and b are its integer arguments. In Python 3, / is float-division regardless of the types of its arguments, and the behaviour of / from Python 2 is recreated by the // operator.

这篇关于相同代码在python3和python2之间的运行结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆