【问题标题】:Program Works in Python 2.7, but not Python 3.3 (Syntax is compatible for both)程序适用于 Python 2.7,但不适用于 Python 3.3(两者的语法兼容)
【发布时间】:2013-02-09 00:36:05
【问题描述】:

所以我在nums.py 中定义了函数integral(function, n=1000, start=0, stop=100)

def integral(function, n=1000, start=0, stop=100):
    """Returns integral of function from start to stop with 'n' rectangles"""
    increment, num, x = float(stop - start) / n, 0, start
    while x <= stop:
        num += eval(function)
        if x >= stop: break
        x += increment
    return increment * num

但是,我的老师(我的编程课)希望我们创建一个单独的程序,该程序使用 input() 获取输入,然后返回它。所以,我有:

def main():
    from nums import integral # imports the function that I made in my own 'nums' module
    f, n, a, b = get_input()
    result = integral(f, n, a, b)
    msg = "\nIntegration of " + f + " is: " + str(result)
    print(msg)

def get_input():
    f = str(input("Function (in quotes, eg: 'x^2'; use 'x' as the variable): ")).replace('^', '**')
    # The above makes it Python-evaluable and also gets the input in one line
    n = int(input("Numbers of Rectangles (enter as an integer, eg: 1000): "))
    a = int(input("Start-Point (enter as an integer, eg: 0): "))
    b = int(input("End-Point (enter as an integer, eg: 100): "))
    return f, n, a, b

main()

在 Python 2.7 中运行时,它运行良好:

>>> 
Function (in quotes, eg: 'x^2'; use 'x' as the variable): 'x**2'
Numbers of Rectangles (enter as an integer, eg: 1000): 1000
Start-Point (enter as an integer, eg: 0): 0
End-Point (enter as an integer, eg: 100): 100

Integration of x**2 is: 333833.5

但是,在 Python 3.3(我的老师坚持我们使用它)中,它会在我的 integral 函数中引发错误,并且输入完全相同:

Traceback (most recent call last):
  File "D:\my_stuff\Google Drive\documents\SCHOOL\Programming\Python\Programming Class\integration.py", line 20, in <module>
    main()
  File "D:\my_stuff\Google Drive\documents\SCHOOL\Programming\Python\Programming Class\integration.py", line 8, in main
    result = integral(f, n, a, b)
  File "D:\my_stuff\Google Drive\Modules\nums.py", line 142, in integral
    num += eval(function)
TypeError: unsupported operand type(s) for +=: 'int' and 'str'

此外,integral 本身(在 Python 3.3 中)也可以正常工作:

>>> from nums import integral
>>> integral('x**2')
333833.4999999991

因此,我相信问题出在我的课程计划中……感谢您提供任何帮助。谢谢:)

【问题讨论】:

    标签: function python-3.x python-2.7


    【解决方案1】:

    您遇到的问题是 input 在 Python 2 和 Python 3 中的工作方式不同。在 Python 3 中,input 函数的工作方式与 Python 2 中的 raw_input 相同。Python 2 的 input 函数相当于 Python 3 中的eval(input())

    由于您使用公式键入的引号,您遇到了麻烦。当您在 Python 2 上运行时键入 'x**2'(带引号)作为公式时,文本会在 input 函数中得到 evaled,结果会得到一个不带引号的字符串。这行得通。

    当您将相同的字符串提供给 Python 3 的 input 函数时,它不会执行 eval,因此引号仍然存在。当您稍后将eval 公式作为积分计算的一部分时,您将得到字符串x**2(不带任何引号)作为结果,而不是x 的平方值。当您尝试将字符串发送到 0 时,这会导致异常。

    要解决此问题,我建议仅使用一个版本的 Python,或将以下代码放在文件顶部以在两个版本中获得 Python 3 样式 input

    # ensure we have Python 3 semantics from input, even in Python 2
    try:
        input = raw_input
    except NameError:
        pass
    

    然后只需键入不带引号的公式,它应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      • 2016-10-08
      • 2012-01-13
      • 1970-01-01
      相关资源
      最近更新 更多