为什么会出现“无效字面值错误”?
我在开发环境里得到了正确的答案,但在线评测系统却给了我以下错误:
Traceback (most recent call last):
File "/tmp/editor_trsource_1410281976_804149.py", line 10, in
n=int(raw_input(''))
ValueError: invalid literal for int() with base 10: '100 10'
问题链接:http://www.hackerearth.com/problem/golf/minimal-combinatorial/
def fact(x):
f=1
while x>0:
f=f*x
x-=1
return f
T=int(raw_input(''))
while T>0:
n=int(raw_input(''))
r=int(raw_input(''))
ans=fact(n)/(fact(r)*fact(n-r))
print str(ans) + "\n"
T-=1
2 个回答
0
正如@John Kugelman提到的,你的程序希望n和r在同一行上。使用sys模块来读取输入会更好。它的工作方式如下:
import sys
def fact(x):
f=1
while x>0:
f=f*x
x-=1
return f
T=int(sys.stdin.readline().strip())
while T>0:
nr = map(int, sys.stdin.readline().strip().split())
#Or you can use nr directly while computing ans
n = nr[0]
r = nr[1]
ans=fact(n)/(fact(r)*fact(n-r))
print str(ans) + "\n"
T-=1
示例运行:
$ python fact.py
1
5 2
10
希望这对你有帮助!
6
n
和 r
需要在同一行输入。
100 10
但是你的程序希望它们在两行中输入。
100
10