2017-11-29 1 views
-1

"goofin.py"프로그램은 사용자에게 목록을 요청하고 홀수를 목록에서 제거하고 새 목록을 인쇄해야합니다.사용자에게 파이썬 코드 입력을 요청하는 경우 EOF 오류

def remodds(lst): 
    result = [] 
    for elem in lst: 
     if elem % 2 == 0:   # if list element is even 
      result.append(elem) # add even list elements to the result 
    return result 


justaskin = input("Give me a list and I'll tak out the odds: ") #this is 
                   #generates 
                   #an EOF 
                   #error 

print(remodds(justaskin))  # supposed to print a list with only even- 
           # numbered elements 


#I'm using Windows Powershell and Python 3.6 to run the code. Please help! 

#error message: 

#Traceback (most recent call last): 
# File "goofin.py", line 13, in <module> 
# print(remodds(justaskin)) 
# File "goofin.py", line 4, in remodds 
# if elem % 2 == 0: 
#TypeError: not all arguments converted during string formatting 
+0

아무 것도 입력 할 수 없거나 입력 한 후에 또는 다른 시간에 오류가 발생합니까? – chepner

+0

Windows Powershell에서 프로그램을 실행할 때 오류가 발생합니다. 즉. 내가 입력 한 후 –

+0

오류를 게시하십시오. 너의 질문에. – TheIncorrigible1

답변

0

이 나를 위해 잘 작동 :

def remodds(lst): 
    inputted = list(lst) 
    result = [] 
    for elem in inputted: 
     if int(elem) % 2 == 0:   
      result.append(elem) 
    return result 


justaskin = input("Give me a list and I'll tak out the odds: ") 
print(remodds(justaskin)) 

내 입력 :

15462625 

내 출력 :

['4', '6', '2', '6', '2'] 

설명 :

여기 내 코드입니다
- convert the input (which was a string) to a list 
- change the list element to an integer 

희망이 있습니다.

0

2, 13, 14, 7 또는 2 13 14 7과 같이 목록을 입력해도 입력 내용이 lst이 아닙니다. 그것은 여전히 ​​하나의 문자열입니다. 여러분이 elem 루프로 분리하면 각 개별 문자는 하나의 루프입니다. 먼저 lst을 분할하고 숫자로 변환해야합니다. 요소는 쉼표 (,)로 예를 들어 분리되어,

def remodds(lst): 
    real_list = [int(x) for x in lst.split()] 
    result = [] 
    for elem in real_list:   #and now the rest of your code 

분할 방법은 순간에 숫자 사이의 공간을 사용하지만, 당신은 또한 정의 할 수 있습니다.

real_list = [int(x) for x in lst.split(',')] 
관련 문제