user = int(raw_input("Type 5 numbers"))
even = []
def purify(odd):
for n in odd:
even.append(n)
if n % 2 > 0:
print n
print purify(user)
Hello I am a beginner and I would like to understand what is wrong with this code.
The User chose 5 numers and I want to print the even numbers only.
Thanks for helping
There are a few problems:
You can't apply int to an overall string, just to one integer at a time.
So if your numbers are space-separated, then you should split them into a list of strings. You can either convert them immediately after input, or wait and do it within your purify function.
Also, your purify function appends every value to the list even without testing it first.
Also, your test is backwards -- you are printing only odd numbers, not even.
Finally, you should return the value of even if you want to print it outside the function, instead of printing them as you loop.
I think this edited version should work.
user_raw = raw_input("Type some space-separated numbers")
user = user_raw.split() # defaults to white space
def purify(odd):
even = []
for n in odd:
if int(n) % 2 == 0:
even.append(n)
return even
print purify(user)
raw_input returns a string and this cannot be converted to type int.
You can use this:
user = raw_input("Input 5 numbers separated by commas: ").split(",")
user = [int(i) for i in user]
def purify(x):
new_lst = []
for i in x:
if i % 2 == 0:
new_lst.append(i)
return new_lst
for search even
filter would be the simplest way to "filter" even numbers:
output = filter(lambda x:~x&1, input)
def purify(list_number):
s=[]
for number in list_number:
if number%2==0:
s+=[number]
return s
Related
I have to iterate through a list using a for loop. In the list, I have to see if the number is even, and if it is, I have to add one to it, and if it is odd, I also have to add one to it. Then i have to print it.
So, the list is [1,2,3,4,5,6,7,8,9,10] and the desired output would be [2,3,4,5,6,7,8,9,10,11].
samplenumber = [1,2,3,4,5,6,7,8,9,10]
for i in samplenumber:
if i%2 == 0:
print (samplenumber+1)
else:
print (samplenumber+1)
This is my code above^^
According to your required Answers, the code will be like this:
samplenumber = [1,2,3,4,5,6,7,8,9,10]
for i in samplenumber:
j=+1
a=int(i)+ j
print (a, end=",")
More accurately, I ask user for a number and after I want to list this number so i can use it for my code!
This is the part of my code i want help:
num=int(raw_input("Give me a number: "))
(*) (*)
#(*)...(*) is the part I want help!
try this :
numbers = []
num = int(raw_input('give me a number'))
numbers.append(num)
print numbers
or keep add items to the list :
numbers = []
while True:
num = int(raw_input('give me a number'))
numbers.append(num)
if the input is 10 20 30 40 50
you ca get the input as follows
number=map(int,raw_input("Enter the list of numbers:").split())
print number
this will have
number=[10,20,30,40,50]
Im taking an online beginner course through google on python 2, and I cannot figure out the answer to one of the questions. Here it is and thanks in advance for your help!
# A. match_ends
# Given a list of strings, return the count of the number of
# strings where the string length is 2 or more and the first
# and last chars of the string are the same.
# Note: python does not have a ++ operator, but += works.
def match_ends(words):
a = []
for b in words:
return
I tried a few different things. This is just where i left off on my last attempt, and decided to ask for help. I have spent more time thinking about this than i care to mention
def match_ends(words):
a = []
for b in words:
if (len(b) > 2 and b[0] == b[len(b)-1]):
a.append(b)
return a
def match_ends2(words):
return [x for x in words if len(x) > 2 and x[0] == x[len(x)-1]]
print(match_ends(['peter','paul','mary','tibet']))
print(match_ends2(['peter','paul','mary','tibet']))
I want to do an algorithm that can generate words of lenght less or equal to n, that doesn't contain certain strings, and that takes less than a minute or two to generate all words when I generate words of lenght 15 or more.
My actual code to generate them is this:
def inv(mob):
if 'aA' in mob:
return False
elif 'Aa' in mob:
return False
elif 'Bb' in mob:
return False
elif 'bB' in mob:
return False
else: return True
S=itertools.product('AaBb',repeat=n)
SS=map(lambda x: ''.join(x),S)
SSS=filter(inv,SS)
(Where n is the lenght)
When I create words of lenght n=10, it takes 3 seconds, but I need to generate at least of lenght n=15 in less than a minute, and when I try with n=15, my computer dies. Someone knows if exists an easy way? Or this problem can't take less time than 2 minutos to generate the words?
Use itertools.imap and itertools.ifilter rather than the map and filter functions as they produce iterables rather than lists. Which should allow avoid creating the large list that map produces which would have to contain 1073741824 (4**15) elements.
S = itertools.product('AaBb', repeat=n)
SS = itertools.imap(lambda x: ''.join(x), S)
SSS = itertools.ifilter(inv, SS)
I have a beginner question. Loops are extremely hard for me to understand, so it's come to me asking for help.
I am trying to create a function to count the amount of even numbers in a user input list, with a negative at the end to show the end of the list. I know I need to use a while loop, but I am having trouble figuring out how to walk through the indexes of the input list. This is what I have so far, can anyone give me a hand?
def find_even_count(numlist):
count = 0
numlist.split()
while numlist > 0:
if numlist % 2 == 0:
count += 1
return count
numlist = raw_input("Please enter a list of numbers, with a negative at the end: ")
print find_even_count(numlist)
I used the split to separate out the indexes of the list, but I know I am doing something wrong. Can anyone point out what I am doing wrong, or point me to a good step by step explanation of what to do here?
Thank you guys so much, I know you probably have something more on your skill level to do, but appreciate the help!
You were pretty close, just a couple of corrections:
def find_even_count(numlist):
count = 0
lst = numlist.split()
for num in lst:
if int(num) % 2 == 0:
count += 1
return count
numlist = raw_input("Please enter a list of numbers, with a negative at the end: ")
print find_even_count(numlist)
I have used a for loop rather than a while loop, stored the outcome of numlist.split() to a variable (lst) and then just iterated over this.
You have a couple of problems:
You split numlist, but don't assign the resulting list to anything.
You then try to operate on numlist, which is still the string of all numbers.
You never try to convert anything to a number.
Instead, try:
def find_even_count(numlist):
count = 0
for numstr in numlist.split(): # iterate over the list
num = int(numstr) # convert each item to an integer
if num < 0:
break # stop when we hit a negative
elif num % 2 == 0:
count += 1 # increment count for even numbers
return count # return the total
Or, doing the whole thing in one line:
def find_even_count(numlist):
return sum(num % 2 for num in map(int, numlist.split()) if num > 0)
(Note: the one-liner will fail in cases where the user tries to trick you by putting more numbers after the "final" negative number, e.g. with numlist = "1 2 -1 3 4")
If you must use a while loop (which isn't really the best tool for the job), it would look like:
def find_even_count(numlist):
index = count = 0
numlist = list(map(int, numlist.split()))
while numlist[index] > 0:
if numlist[index] % 2 == 0:
count += 1
index += 1
return count