Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在函数中输入列表?用python_Python - Fatal编程技术网

如何在函数中输入列表?用python

如何在函数中输入列表?用python,python,Python,编写一个名为“find_even_count”的函数,该函数允许用户在键盘上输入任意正整数序列,然后打印用户输入的正偶数整数数。数字的顺序在开始时是未知的。用户可以输入负数以显示序列的结束。 例如:如果用户输入序列, 1, 3, 5, 23, 56, 14, 68, 25, 12, -1 然后您的函数需要打印“4个偶数”,因为序列中有4个偶数。 提示:使用while循环 这是我的密码 find_even_count(x): i = x even_count = 0 whi

编写一个名为“find_even_count”的函数,该函数允许用户在键盘上输入任意正整数序列,然后打印用户输入的正偶数整数数。数字的顺序在开始时是未知的。用户可以输入负数以显示序列的结束。 例如:如果用户输入序列, 1, 3, 5, 23, 56, 14, 68, 25, 12, -1 然后您的函数需要打印“4个偶数”,因为序列中有4个偶数。 提示:使用while循环

这是我的密码

find_even_count(x):
    i = x
    even_count = 0
    while x> 0:
            if i%2 ==0:
                    even_count+=1

    print even_count
我一直收到错误代码

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    find_even_count(2,22,224,24,-1)
  File "/Users/rayhow/Desktop/assignment2_Q4.py", line 5, in find_even_count
    if i%2 ==0:
TypeError: unsupported operand type(s) for %: 'tuple' and 'int'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
求偶数(2,22224,24,-1)
文件“/Users/rayhow/Desktop/assignment2_Q4.py”,第5行,查找偶数
如果i%2==0:
TypeError:不支持%的操作数类型:'tuple'和'int'
为什么我会收到这个错误消息?

给你,我的朋友

def find_even_count(x):
    even_count = 0
    for n in x:
            if n%2 ==0:
                    even_count+=1

    print even_count
input_list = raw_input('give me list of numbers: ')
print(filter(lambda x: x.isdigit() and not int(x) % 2, input_list.split()))
我会给你

(ocmg)brunsgaard@archbook /tmp> python pos.py
give me list of numbers: 1 2 3 4 -2 -1 your mother 54
['2', '4', '54']
如果你想要一个更简单的解决方案,也可以选择

even = lambda l: len([x for x in l if not x % 2 and x > 0])
这将输出

even([1, -2, 3, 4, 6, 5])
2

因为有两个偶数

你的代码中缺少了
def
,还是在复制时被截断了?一定是被截断了。我想列出一组整数,计算偶数,当我输入一个负整数时,脚本结束了。这是一个家庭作业问题吗?我没有复习过其中的任何一个这些命令还只是基本的while和for循环。@Ray RayHow,很好:)只需使用您觉得最舒服的答案即可。。这个解决方案有点先进,但在我看来也是最好的;)@Ray RayHow,我改进了我的答案,为您提供了另一个解决方案这将无法通过家庭作业测试,因为它不允许用户输入数字。无论如何,这个问题都不会要求解决家庭作业问题,而是通过错误信息寻求帮助。