猿问

OJ 说我的 python 程序编译错误,即使它在我的电脑上运行良好

我为我的作业编写了一个简单的 python 程序,它希望我们将输入的三个整数相加。我将它提交给我学校的 oj(在线法官)系统,但我收到了“编译器错误”


该程序在运行 ubuntu 18.04 amd64 的个人计算机上运行良好,但我无法通过 oj 测试。我真的不知道出了什么问题,因为 oj 没有给出任何消息,只有最终状态“编译器错误”


import sys


a = input()

b = input()

c = input()

a = int(a)

b = int(b)

c = int(c)

answer = a + b + c

print(f"The answer is {answer}")

这是我在计算机上运行程序时的结果:


ubuntu@VMware:~/python-intro $ python3 1003.py

3

4

5

The answer is 12

和我的电脑系统:


ubuntu@VMware:~/python-intro $ lsb_release -a

No LSB modules are available.

Distributor ID: Ubuntu

Description:    Ubuntu 18.04.1 LTS

Release:        18.04

Codename:       bionic

怎么会是 oj 上的编译器错误?我已经确认我的提交语言是“python 3”而不是“python 2”或其他语言。


哆啦的时光机
浏览 470回答 3
3回答

湖上湖

也许你的 OJ 的 python 3 版本不是 python 3.6。并且您不能在低于 3.6 的 Python 版本上使用 f-string。我建议将您的字符串格式更改为:print("The answer is {answer}".format(answer=answer))

ITMISS

正如阿马丹指出的那样,问题主要与 f-string 有关。print(f"答案是{answer}") <<<使用格式使用更改此设置并更新线程。print("答案是{}".format(answer))

慕桂英546537

f 字符串是在 Python 3.6 中引入的。在早期的 Python 中,这一行是一个语法错误:print(f"The&nbsp;answer&nbsp;is&nbsp;{answer}")为确保兼容性,将其重写为print("The&nbsp;answer&nbsp;is&nbsp;%s"&nbsp;%&nbsp;answer)
随时随地看视频慕课网APP

相关分类

Python
我要回答