【问题标题】:General Python Errors on Yosemite优胜美地的一般 Python 错误
【发布时间】:2015-03-03 22:19:58
【问题描述】:

我对 python 非常陌生(作为大学一年级的课程),我按照课程召集人的建议安装了 WindIDE,以及用于 Mac OS 10.6 的 Python 3.4.2 包。我正在 13 英寸 Macbook Air 上运行 Yosemite,如果这些信息有帮助的话。

我遇到的主要问题是,当我尝试执行几乎任何类型的代码时,似乎都会从各个角度向我抛出错误。起初,我将问题归咎于我犯了语法和逻辑错误,但我很快得出结论,事实并非如此,因为即使我的讲师为我们提供的模型示例也无法运行。

以下是我尝试运行的代码示例以及输出:

number_1 = eval (input ("Enter the first number: "))
number_2 = eval (input ("Enter the second number: "))

sum = number_1 + number_2

print ("The sum of the numbers",number_1,"and",number_2,"is",sum)



Enter the first number: 2
Traceback (most recent call last):
  File "/Users/damonlurie/Documents/CSC1005F/Examples/Python Basics/add.py", line 5, in 0
TypeError: eval() arg 1 must be a string or code object

我显然不知道为什么会这样

【问题讨论】:

    标签: python macos eval osx-yosemite


    【解决方案1】:

    input() 已经在输入中使用了eval()。你可以去掉eval(),或者用raw_input()替换input()

    使用

    number_1 = input("Enter the first number: ")
    number_2 = input("Enter the second number: ")
    sum = number_1 + number_2
    

    number_1 = eval(raw_input("Enter the first number: "))
    number_2 = eval(raw_input("Enter the second number: "))
    sum = number_1 + number_2
    

    【讨论】:

      【解决方案2】:

      Macintosh 计算机预装了 Apple 特定的 Python 2 端口。很可能,您的脚本作为 Python 2 脚本执行,而不是您安装的 Python 3。尝试按照this 视频中的说明进行配置。

      【讨论】:

        猜你喜欢
        • 2014-12-16
        • 1970-01-01
        • 2014-10-04
        • 2014-12-13
        • 1970-01-01
        • 1970-01-01
        • 2015-03-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多