[英]How do I make this random list print only even numbers
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.
第二个 function 必须使该随机列表仅生成偶数或偶数。 (I'm in python 2 but python 3 might help too) Here's my code:
(我在 python 2 但 python 3 也可能有帮助)这是我的代码:
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
:您需要从
random_list
return
数字列表:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.