2014-09-26 4 views
-1

나는 Python (Windows 7에서 Python 3.4)을 배웠다. pycharm IDE에서 다음 코드를 실행하면 잘 작동하지만 코드가 codeeval.com에서 해결 방법으로 제출되면 IOError 오류가 발생합니다. [Errno 2] 해당 파일이나 디렉토리가 없습니다. 'D :/Trial/simple.txt '. 왜 그렇게? 온라인 인터프리터 (Codeeval 제출 창)에서 로컬 디스크에있는 파일을 읽는 올바른 방법은 무엇입니까?로컬 디스크에서 파일 읽기

path = "D:/Trial/simple.txt" 
file = open(path,"r+") 
age = file.read().split() 
for i in range(0,len(age)): 
    if int(age[i]) <= 2: 
     print("Still in Mama's arms") 
    if 4 == int(age[i]) == 3: 
     print("Preschool Maniac") 
    if 5 <= int(age[i]) >= 11: 
     print("Elementary school") 
    if 12 <= int(age[i]) >= 14: 
     print("Middle school") 
    if 15 <= int(age[i]) >= 18: 
     print("High school") 
    if 19 <= int(age[i]) >= 22: 
     print("College") 
    if 23 <= int(age[i]) >= 65: 
     print("Working for the man") 
    if 66 <= int(age[i]) >= 100: 
     print("The Golder years") 
file.close() 
+0

대부분의 경우, 입력은'stdin '을 통해 제공됩니다. – dawg

+0

나는 codeeval 인터프리터가 파일을 읽을 수 없다고 생각합니다. 확실히 컴퓨터에서 파일을 읽을 수 없으므로 보안상의 이유로 서버에서 파일을 읽을 수 없습니다. – Blckknght

답변

0

연결 조건은 예상 한대로 작동하지 않습니다. a compare b compare c == a compare b and b compare c. 따라서 4 == int(age[i]) == 3은 사실이 아니며 5 <= int(age[i]) >= 11 == int(age[i]) >= 11입니다.

연령이 한 줄에 있는지, 아니면 한 줄로되어 있는지를 지정하지 않았습니다. 어느 쪽이든 다음과 같이 작동해야합니다. range을 사용하지 않고 목록을 직접 반복 할 수 있습니다.

import sys 
ages = sys.stdin.read().split() 
for age in sys.stdin.read().split(): 
    age = int(age) 
    if age <= 2: 
     print("Still in Mama's arms") 
    if 3 <= age <= 4: 
     print("Preschool Maniac") 
    if 5 <= age <= 11: 
     print("Elementary school") 
    ... 
관련 문제