简体   繁体   English

我正在制作一个函数,它返回 FizzBu​​zz 列表中所有数字的总和。 我认为这会很好但不起作用。 在 Python 中

[英]I'm making a function that return the sum of the all number in the FizzBuzz list. I thought it would be fine but not working. In Python

The result is 0 It should be 60 if I type 15 but not.结果是 0 如果我输入 15 而不是,它应该是 60。 I don't know why but n_numlist is empty.我不知道为什么,但 n_numlist 是空的。 Where is my mistake?我的错误在哪里? I cannot find it!!我找不到它了!! Is there someone who can find?有人能找到吗? Please help me!!请帮我!!

#This is my code:

def to_fizzbuzz(number):
    if number % 15 == 0:
        return 'FizzBuzz'

    if number % 3 == 0:
        return 'Fizz'

    if number % 5 == 0:
        return 'Buzz'

    else:
        return str(number)
        # return i

def main():
    N = int(input())
    # this list concludes "FizzBuzz", "Fizz" or "Buzz"
    fblist = []
    for number in range(1, 10**6):
        result = to_fizzbuzz(number)
        fblist.append(result)

    # the list up to N
    n_list = fblist[0:N]
    # this list contains only numbers and up to N

    n_numlist = []

    for s in n_list:
        if s.isdigit == True:
            n_numlist.append(s)

    print(sum(n_numlist))


main()

You need to change your for s in n_list loop to this:您需要将for s in n_list循环中的for s in n_list更改for s in n_list

for s in n_list:
    if s.isdigit() == True:
        n_numlist.append(int(s))

Because isdigit() is a function, you need to add () to the end of it to call it.因为isdigit()是一个函数,所以需要在它的末尾加上()才能调用它。

Because you return your numbers as strings - return str(number) - it can't sum them, so you need to convert them back to integers when you append them to n_numlist .因为您将数字作为字符串return str(number) - return str(number) - 它无法对它们求和,因此当您将它们附加到n_numlist时,您需要将它们转换回整数。

x = int(input())

inputList = list(range(1,x+1)) #creating a list [1,x]
stringList = [str(a) for a in inputList] #changing int elements to string

for n, i in enumerate(stringList): #n shows which element from list, i shows this element

    if int(i) %15 == 0: #changing to int because we are doing math
        stringList[n] = "FizzBuzz" 

    elif int(i) %5 == 0:
        stringList[n] = "Fizz"

    elif int(i) %3 == 0:
        stringList[n] = "Buzz"

print(stringList) #let's see our new list

last_list = [] #lets add output list

for a in stringList: 
        if a.isdigit() == True: #you forgot put () to isdigit by the way 
            last_list.append(a) 

int_last_list = [int(a) for a in last_list] #we are changing all elements to int because we are going to do some math.

print(sum(int_last_list))

暂无
暂无

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

相关问题 Python - 我不确定为什么我定义的这个函数不起作用。 对编程很新鲜 - Python - I'm not sure why this function I'm defining isn't working. Very fresh to programming 总结列表中的十进制数。 Python - sum the decimal number in list. python 我如何能够返回哪个实例属于列表中的随机数。 不使用百万个if语句? - How would I be able to return which instance belongs to the random number in my list. Without using a million if statements? 我的偶数python函数索引仅返回列表中的第一个偶数。 如何使其返回列表中所有偶数的索引? - My indices of evens python function only returns the first even number in a list. How to make it return the index of all even numbers in a list? 我如何将所有这些变量转换成一个列表。 我可以在哪里调用 python 中的随机电影,或者有更好的方法来解决这个问题吗? - How would i convert all these variables into a list. where i could call a random film in python or is there a better way to sort through this? 如何在python中定义函数以选择列表。 - How do I define a function in python to choose a list. 我想计算欧几里得距离并将其放入列表中。 我收到范围错误,我错过了什么? - I would like to calculate euclidian distance and put it in a list. I recive range error, what I'm missing? python中的图像处理-效果不佳 - image processing in python - not working as well as I thought 需要从列表中删除重复项。 Set() 函数不起作用。 也不是 for 循环方法 - Need to remove duplicates from a list. Set() function isn't working. Neither is a for loop method 我如何通过 Python 中的这个断言测试来尝试找到字典中列出的所有视图数量的总和? - How would I get past this assertion test in Python for trying to find the sum of all the number of views listed in a Dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM