简体   繁体   English

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

[英]OJ says compile error about my python program even if it runs well on my computer (ubuntu 18.04)

i wrote a simple python program for my homework that wants us to add up three integers given from input.我为我的作业编写了一个简单的 python 程序,它希望我们将输入的三个整数相加。 i submitted it to my school's oj (online judge) system, but i got "compiler error"我将它提交给我学校的 oj(在线法官)系统,但我收到了“编译器错误”

the program runs very well on my personal computer that runs ubuntu 18.04 amd64, but i can't get it pass the oj test.该程序在运行 ubuntu 18.04 amd64 的个人计算机上运行良好,但我无法通过 oj 测试。 i sincerely don't know what went wrong because the oj didn't give any message, only a final status "compiler error"我真的不知道出了什么问题,因为 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}")

here's the result when i run my program on my computer:这是我在计算机上运行程序时的结果:

ubuntu@VMware:~/python-intro $ python3 1003.py
3
4
5
The answer is 12

and my computer system:和我的电脑系统:

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

how can it be a compiler error on the oj??怎么可能是 oj 上的编译器错误? i have confirmed that my submission language is "python 3" not "python 2" or whatever.我已经确认我的提交语言是“python 3”而不是“python 2”或其他语言。

Maybe the python 3 version of your OJ is not python 3.6.也许你的 OJ 的 python 3 版本不是 python 3.6。 And you can't use f-string on python versions below 3.6.并且您不能在低于 3.6 的 Python 版本上使用 f-string。

I suggest to change your string format to:我建议将您的字符串格式更改为:

print("The answer is {answer}".format(answer=answer))

f-strings were introduced in Python 3.6. f 字符串是在 Python 3.6 中引入的。 In an earlier Python, this line is a syntax error:在早期的 Python 中,这一行是一个语法错误:

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

To ensure compatibility, rewrite it to为确保兼容性,将其重写为

print("The answer is %s" % answer)

The problem is mostly related to f-string as Amadan pointed out.正如阿马丹指出的那样,问题主要与 f-string 有关。

print(f"The answer is {answer}") <<< print(f"答案是{answer}") <<<

change this with format usage and update the thread.使用格式使用更改此设置并更新线程。

print("The answer is {}".format(answer)) print("答案是{}".format(answer))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的程序运行良好,但为什么它退出后给我一个属性错误? - My program runs well, but why is it giving me an Attribute Error after it exits? 连续运行python代码后,我的计算机最终崩溃 - Upon successive runs of my python code eventually crashes my computer 关于 Ubuntu 18.04 LTS 和 python 的问题 - Question about Ubuntu 18.04 LTS and python 我的代码在我的计算机上完美运行,但在服务器上执行时却不行? PYTHON - My code runs perfectly on my computer but not when executed on server? PYTHON 为什么这个 python 代码可以在我的 XUbuntu(Ubuntu 20.04)机器上工作,但不能在我的 Ubuntu 18.04 服务器上工作 - Why does this python code work on my XUbuntu (Ubuntu 20.04) machine but not my Ubuntu 18.04 Server 无法在ubuntu 18.04中将python程序作为服务运行 - Cannot run python program as a service in ubuntu 18.04 Ubuntu 18.04上的VSCode无法运行Python程序 - VSCode on Ubuntu 18.04 cannot run a Python program 为什么我的程序不给出 output 甚至错误 python - Why doesn't my program give output or even error in python Python - 我的程序出错 - Python - Error in my program 为什么我的Python程序会减慢计算机速度 - why is my Python program slowing down my computer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM