简体   繁体   English

显示并返回函数上重复字符串的数量?

[英]Showing and returning the number of a repeated string on a function?

I'm already learning Python on a online free course. 我已经在线免费课程学习Python了。 Now I have and exercise that I've been hours trying to do but.... I cannot see why it doesn't works.. 现在我已经锻炼了,我已经花了好几个小时试着做但......我不明白为什么它不起作用..

The instructions are here: Write a function called fizz_count that takes a list x as input and returns the count of the string “fizz” in that list. 说明如下:编写一个名为fizz_count的函数,它将列表x作为输入,并返回该列表中字符串“fizz”的计数。

For example, fizz_count(["fizz","buzz","fizz"]) should return 2 . 例如, fizz_count(["fizz","buzz","fizz"])应返回2 (Make sure your function return s the number instead of print ing it.) Check out the Hint if you need help! (确保您的函数return数字而不是print它。)如果您需要帮助,请查看提示!

Hint: Try making a counter variable (for example, count ) in your function. 提示:尝试在函数中创建counter变量(例如, count )。 The counter variable could initially be set to zero. 计数器变量最初可以设置为零。 After that, you can loop through the list that you take as input and increase count by one every time an item in the list is equal to the string "fizz". 之后,您可以循环浏览作为输入的列表,并在每次列表中的项目等于字符串“fizz”时将计数增加1。

At the end, don't forget to return the number of "fizz" s! 最后,不要忘记return "fizz"的数量!

Then here's my code: 那么这是我的代码:

x=["fizz","bear","fizz"]

def fizz_count(*x):
    count=str(0)
    for count in x:
        if x=='fizz':
            count=count+str(1)
        return count

print fizz_count(x) #It prints "fizz","bear","fizz" but the thing that I want is to                 
                    #print "2" because "fizz" is two times in that string..

To fix your specific issue: 要解决您的具体问题:

x=["fizz","bear","fizz"]

def fizz_count(x):
    count= 0
    for e in x:
        if e=='fizz':
            count = count + 1
    return count

print fizz_count(x)

A few issues: 一些问题:

  1. No need of *x in the parameter in python python中的参数不需要*x
  2. Use a different variable name when you are iterating through the elements. 在迭代元素时使用不同的变量名。
  3. count=0 is sufficient, no need to cast it as a string count=0就足够了,不需要将其转换为字符串
  4. if x=='fizz' should be if e=='fizz' - Check the element of the list, rather than the entire list. if x=='fizz'应该是if e=='fizz' - 检查列表的元素,而不是整个列表。
  5. The return statement had to be after the loop executes. return语句必须是循环执行 Notice the indentation. 注意缩进。

Ofcourse, there are better ways of achieving what you are looking for, but I shall leave it here as it seems like you are learning. 当然,有更好的方法来实现你想要的东西,但我会把它放在这里,因为你似乎在学习。

Hope this helps. 希望这可以帮助。

Few things about your script: 你的脚本几乎没有:

def fizz_count(*x)

This probably isn't the syntax you want here. 这可能不是您想要的语法。 What you're doing here is expanding the list so it takes its own argument slot. 你在这里做的是扩展列表,因此它需要自己的参数槽。 You probably want to pass it like this: 您可能希望像这样传递它:

def fizz_count(x)

Next: 下一个:

count = str(0)

You can simply initiate your counter like this: 您可以像这样简单地启动您的计数器:

count = 0

Next: 下一个:

for count in x:

This now makes your count variable an entry in x , but since you expanded the list this probably isn't what you're seeing. 这现在使你的count变量成为x一个条目,但是由于你扩展了列表,这可能不是你所看到的。 Regardless, again, you want something like this to assign each member to a variable: 无论如何,你想要这样的东西将每个成员分配给一个变量:

for item in x:

Next: 下一个:

count=count+str(1)

Again, you don't need to convert this to a string, count=count+1 would suffice 同样,您不需要将其转换为字符串, count=count+1就足够了

Finally, not sure if it's just a formatting issue, but your return statement should be outside your for-loop. 最后,不确定它是否只是一个格式化问题,但是你的return语句应该在你的for循环之外。

First of all, you shouldn't really have x=["fizz","bear","fizz"] before the function, you should have it after as it makes more sense to have it together with the print function. 首先,你不应该在函数之前真正拥有x=["fizz","bear","fizz"] ,你应该拥有它,因为将它与print函数结合起来更有意义。 I've tested it, this should work: 我测试了它,这应该工作:

def fizz_count(x):
 count=0
 for i in x:
  if i=='fizz':
   count+=1
 return count

x=["fizz","bear","fizz"]
print (fizz_count(x))

What I don't understand is why you had count as a string. 我不明白为什么你count一个字符串。 You don't need to. 你不需要。 Just make it an ordinary integer and the variable i in automatically created when I declare this for loop. 只需使它成为一个普通的整数,并在我声明这个for循环时自动创建变量i Also, you shouldn't have count=count+1 as although it still works, it isn't really the pythonic way of doing this and there's a reason the += operator was built in( it does the same thing but makes your life that little bit easier) 此外,你不应该有count=count+1 ,虽然它仍然可以工作,但它并不是真正的pythonic方式,并且有一个原因是+=运算符被内置(它做同样的事情,但让你的生活那更容易一点)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM