Reputation: 25
I got a question which says "Write a function user prints even number from list). We have to ask the user to input a list. I am getting a "Type Error: not all arguments converted during string formatting". Please help were am I wrong.
def even_no(x):
a = x.split()
new_list = []
for i in a:
if i % 2 == 0:
new_list.append(i)
input_no = input("Enter number sequence: ")
print(even_no(input_no))
Upvotes: 0
Views: 68
Reputation: 1972
Your indentation
was a bit so I fixed that and you also forgot to use return
. I fixed all of that including your TypeError
. Here is the code:
def even_no(x):
a = x.split()
new_list = []
for i in a:
if int(i) % 2 == 0:
new_list.append(i)
return new_list
input_no = input("Enter number sequence: ")
print(even_no(input_no))
Upvotes: 0
Reputation: 36
A couple of issues to be noted. The first one has already been mentioned, which is the need to convert strings to integers. The second is with the following line: input_no = input("Enter number sequence: ") . When I tested, the function "even_no" would not execute with the above line present as a global variable. To overcome this problem I used tkinter and a class in the following code. Please note: This allows the user to enter a number or numbers into an entry field. Each number should be separated by a space. Only numbers should be used. If any even numbers are entered, they will be printed into a list in the python shell. If No even numbers are entered, an empty list will be printed. If non-numeric characters are entered, the Value Error is handled to guide the user on the correct method of input.
import tkinter as tk
from tkinter import Tk, messagebox
import tkinter.filedialog
from tkinter import Tk, Label, Button
from tkinter import *
class Control(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.controller = self
self.shared_data = {
"input_no": tk.StringVar(),
}
self.title('Even')
self.entry = Entry(self, textvariable=self.shared_data["input_no"])
self.entry.pack()
self.enterbutton = tk.Button(self, text='Enter',
command=(self.even_no)
)
self.enterbutton.pack()
def even_no(self):
try:
user_input = self.shared_data["input_no"].get()
a = user_input.split()
new_list = []
for i in a:
if int(i) % 2 == 0:
new_list.append(int(i))
print(new_list)
except ValueError:
print('Invalid Entry. Please enter numbers only. \n'
'Please make sure that each number is separated by a space.')
Control1 = Control()
Control1.mainloop()
Upvotes: 0
Reputation: 1057
%
is also use for string formatting, and since the split
method belongs to string
, it also returns a string
, hence, the interpreter tries to format it.
change the line:
if i % 2 == 0:
to
if int(i) % 2 == 0:
that your code should work.
As a side note, your function won't print anything because there's no return for the even_no
function
Upvotes: 1
Reputation: 5833
You forgot to convert the numbers from str
to int
. map
can be used for this. Also you did not return your list.
def even_no(x):
a = map(int, x.split())
new_list = []
for i in a:
if i % 2 == 0:
new_list.append(i)
return new_list
input_no = input("Enter number sequence: ")
print(even_no(input_no))
Execution example:
Enter number sequence: 10 20 30
[10, 20, 30]
Upvotes: 0