2017-11-29 7 views
-1

プログラム "goofin.py"はユーザーにリストを要求し、奇数をリストから削除して新しいリストを印刷することになっています。ユーザーにPythonコードの入力を要求するときの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ループでそれを取るとき、それぞれの文字が1つのループであることを意味する、まだ1つの文字列です。最初に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(',')] 
関連する問題