i want to make program that it get a list of numbers from user and then find even numbers and odd numbers and print it.here is my code.please help me.i want my output should be list.this code has error now!!!
def find_evens_and_odds()
numbers=list(input("give me your list:"))
evens=list()
odds=list()
for i in numbers:
if i % 2 ==0:
evens.append(i)
else:
odds.append(i)
print(numbers)
print(evens,odds)
import ast
def find_evens_and_odds():
number=input("give me your list ") # Enter like this [1,2,3]
numbers= ast.literal_eval(number)
evens=[]
odds=[]
for i in numbers:
if int(i) % 2 ==0:
evens.append(i)
else:
odds.append(i)
print(numbers)
print(evens,odds)
find_evens_and_odds()
def find_evens_and_odds(numbers):
evens = list()
odds = list()
for i in numbers:
if i % 2 == 0:
evens.append(i)
else:
odds.append(i)
print(numbers)
print(evens, odds)
numbers = list(map(int,input().split()))
find_evens_and_odds(numbers)
Related
I wrote this code to print how many prime numbers there are in a given list of numbers,
but it is not outputting anything. What am I doing wrong?
def count_primes(nums):
primes = 0
number_of_primes = 0
a_list = []
listing = 0
a_list == nums
for x in a_list:
if a_list % 2 == 0:
primes = primes + a_list
listing == len(primes)
print(listing)
You haven't called the function which is why you're not getting an output.
You may try this code to print the prime numbers at a given range.
from math import sqrt
def isPrime(x):
if x == 2:
return True
if x < 2:
return False
for i in range(2, int(sqrt(x))+1):
if x % i == 0:
return False
return True
n = int(input())
for i in range(1, n+1):
if isPrime(i):
print(i, end=" ")
I tried to find even numbers in a string
s = "75,41,14,8,73,45,-16"
evenNumbers = []
for number in s.split(","):
if int(number) % 2 == 0 and int(number) > 0:
evenNumbers += number
evenNumbers = ','.join(evenNumbers)
print("Even Numbers : \"{}\"".format(evenNumbers))
and its output is like
Even Numbers : "1,4,8"
I want to make it like
Even Numbers : "14,8"
You can append the number to the list. Something like this:
s = "75,41,14,8,73,45,-16"
evenNumbers = []
for number in s.split(","):
if int(number) % 2 == 0 and int(number) > 0:
evenNumbers.append(number)
print("Even Numbers : \"{}\"".format(evenNumbers))
"['14', '8']"
You need to add numbers, not strings to the list
evenNumbers += int(number)
Then join strings
evenNumbers = ','.join(map(str, evenNumbers))
Otherwise, do evenNumbers.append(number)
Or do it all on one line
evenNumbers = ','.join(filter(lambda x: int(x) % 2 == 0 and int(x) > 0, "75,41,14,8,73,45,-16".split(",")))
You will take expected output with this. All you need to do to replace evenNumbers += number with evenNumbers.append(int_num).
s = "75,41,14,8,73,45,-16"
evenNumbers = []
for number in s.split(","):
int_num = int(number)
if int_num % 2 == 0 and int_num > 0:
evenNumbers.append(int_num)
print("Even Numbers : \"{}\"".format(evenNumbers))
you could also do a list comprehensions
even_numbers = [n for n in s.split(',') if int(n) % 2 == 0]
I am intended to construct a list of strings from list of numbers. For example, the given list is
list=[1,2,5,25,6]
Desired output:
['Odd', 'Even', Odd, 'multiples of 5 and odd', 'multiples of 2 and even']
My work so far:
list=[]
for num in numbers:
if num % 2:
list.append('Odd')
if not num % 5:
list.append('multiples of 5 and odd')
else:
if not num % 5:
list.append('multiples of 2 and even')
else:
list.append('even')
print(list)
It printed the list but in a wrong way. I was wondering you if you could review my code. Thanks for your help!
l = []
for num in numbers:
if num % 2 == 0:
# Even numbers
# Every even number is a multiple of 2 except number 0
if num == 0:
l.append('Even')
else:
l.append('Multiples of 2 and even')
else:
# Odd numbers
if num % 5 == 0:
l.append('Multiples of 5 and odd')
else:
l.append('Odd')
print(l)
I need to make a program in python that generates ten random numbers from 1-100 that stores it in a list using a loop. Then a second loop should display said list, then calculate the sums of the even and odd elements to display them. This is what I have so far, any help is greatly appreciated. Thanks
import random
def main():
numlist = [0] * 10
for r in range(10):
numlist[r] = random.randint(1,100)
print(numlist)
list_length = len(numlist)
print('The number of elements in the list is', list_length)
More specifically this is the part I'm stuck on. I have to add the sums of the odd and then even elements. Every work around I've tryed has only given me the sum of the total elements.
for x in range(0, 10, 2):
numlist[x] = numlist
print('The Sum of the odd numbers is ', sum(numlist))
main()
import random
nums = [random.randint(1,100) for _ in range(10)]
You can use lambdas and filter
evenSum = sum(filter(lambda i : i%2 == 0, nums))
oddSum = sum(filter(lambda i : i%2, nums))
Or make some quick helper functions
def isEven(x):
return x % 2 == 0
def isOdd(x):
return x % 2 == 1
evenSum = sum(filter(isEven, nums))
oddSum = sum(filter(isOdd, nums))
Using your own code:
def main():
numlist = [] # create empty list to add randints to
for r in range(10):
numlist.append(random.randint(1,100)) # append to the list
list_length = len(numlist)
print('The number of elements in the list is', list_length)
odd = 0
even = 0
for num in numlist:
if num % 2: # if num mod 2 has a remainder, num is odd
odd += num
else: # else num is even
even += num
print('The Sum of the odd numbers is {} and even numbers is {}'.format(odd,even))
You can replace the first loop with a list comp:
numlist = [random.randint(1,100) for _ in range(10)]
Can't Understand the actual problem (hahehehe !)
As far as I have understood, you want to print the sum of odd and even numbers of the list that is generated from randint(). Well I have just edited your code ;)
so here is the simple done code !
Vote If It Helps !
import random
def main():
odd_sum=0
even_sum=0
numlist = [0] * 10
for r in range(10):
numlist[r] = random.randint(1,100)
print numlist
list_length = len(numlist)
print('The number of elements in the list is', list_length)
for i in numlist:
if (i%2 == 1): #the remainder is 1 if the number is odd
odd_sum = odd_sum + i #add the odd_numbers
elif(i%2 == 0): #the remainder is 0 if the number is even
even_sum = even_sum + i #add the even_numbers
else:
print "No need of this else part !"
print "Odd_sum = ",odd_sum
print "Even_sum = ",even_sum
main()
I'm trying to write a program that picks out the primes in a range from 3 to 9. Here's my code:
primes_list = []
number = 3
while number > 2 and number < 10:
for n in range(2, number):
if number % n == 0:
break
number += 1
else:
primes_list.append(number)
print primes_list
This seems to be an infinite loop but that's just a guess because the output never shows up. If it is an infinite loop, why?
Also I was wondering if there's some sort of list comprehension that can pick out only prime numbers? Maybe list comprehensions is the more efficient way to go? only I haven't figure out how to just filter the primes.
Any help or comment is deeply appreciated.
while number > 2 and number < 10:
for n in range(2, number):
if number % n == 0:
break
else:
primes_list.append(number)
number += 1
>>> primes_list
[3, 5, 7]
>>>
This is probably what you had in mind.
Note that number += 1 inside the first loop was misplaced
for n in range(2, number):
if number % n == 0:
break
number += 1
Because of the break, you never get to
number += 1
when that condition is met. It will just keep appending number to the list forever.
Try something along the lines of:
primes_list = []
lower = 3
upper = 10
for number in range(lower, upper):
if(isPrime(number)):
primes_list.append(number)
print primes_list
#note this is not an efficient implementation
def isPrime(number):
for n in range(2, number):
if number % n == 0:
return false
return true