I'm trying to make a program but it keeps doing something wrong. The program I'm trying to make is two functions. One that makes a random list(it returns it not prints it). The second function has to make that random list generate only even integers or numbers. (I'm in python 2 but python 3 might help too) Here's my code:
def random_list():
res = random.sample(range(0, 50), 6)
print str(res)
random_list()
def even_numbers():
if (2 / 2 == 0):
print(2)
even_numbers()
#What am I doing wrong?
You need to return
the list of numbers from random_list
:
import random
def random_list():
res = random.sample(range(0, 50), 6)
return res
def even_numbers(nums):
for num in nums:
if (num % 2 == 0):
print(num)
my_numbers = random_list()
even_numbers(my_numbers)
Out:
48
36
22
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.