【问题标题】:Seemingly perfect code written on TextWrangler gives SyntaxError when run on Python 3在 TextWrangler 上编写的看似完美的代码在 Python 3 上运行时会出现 SyntaxError
【发布时间】:2016-05-07 00:13:06
【问题描述】:

这是我的代码:

def trip(nights):
    return 140 * nights

def plane_cost(ride):
    if ride == "London":
        return 220
    elif ride == "Rome":
        return 200
    elif ride == "Glascow":
        return 185

def car_rental(days):
    cost = 20 * days
    if days >= 7:
        cost = cost - 25
    if days >= 3:
        cost = cost - 10
    return cost

def tripcost(ride, days, spending_pounds):
    trip(days) + plane_cost(ride) + car_rental(days) + spending_pounds

print tripcost("London", 5, 500)

想知道为什么当我在 idlE 中加载单词 tripcost(以粗体标记)时显示为语法错误?

【问题讨论】:

  • 我打赌 TextWrangler 是 Python 2 并且您的 IDE 连接到 Python 3 的安装。是这样吗?尝试将最后一行更改为print(tripcost("London", 5, 500))
  • @JRazor - 我认为 OP 只是用星号指出错误所在。问题中没有其他任何迹象表明需要粗体文本。
  • edit您的帖子并添加语法错误的全文
  • 除非 IDE 做一些额外的事情,而 IDLE 没有,SyntaxErrors 来自 python x.y 解析器/编译器。同样,使用的编辑器是无关紧要的,除非它改变了你输入的内容,而 IDLE 也不会。如果报告准确,这一定是 2 对 3 语法更改问题。

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


【解决方案1】:

尝试在脚本开头添加from __future__ import print_function并在print(tripcost("London", 5, 500))上更改print tripcost("London", 5, 500)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    相关资源
    最近更新 更多