The result is 0 It should be 60 if I type 15 but not. I don't know why but n_numlist is empty. 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:
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.
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
.
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))
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.