Inserting a string into a list of ints

0

I have the following rule:

Create a function that receives a list of integers, and replace the items as below:

  • If Multiple of 3 = 'Fizz'
  • if multiple of 5 = 'Buzz'
  • if multiple of 3 and 5 = 'Fizzbuzz'

I created the program below:

    intList = list()
intList = [1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]
newlist=list()

def fizzbuzz( lista ):
   for item in lista:
       if item % 3 == 0 and item % 5 == 0:
           lista.remove(item)
           newlist.append(item) ='fizzbuzz'
       elif  item % 3 == 0:
           lista.remove(item)
           newlist.append(item) ='fizz'
       elif  item % 5 == 0:
           lista.remove(item)
           newlist.append(item)='buzz'
       else:
           newlist.append(item)=item


fizzbuzz(newlist)

But returns the following error:

C: \ Python27 \ my_scripts> for_loop.py   File "C: \ Python27 \ my_scripts \ for_loop.py", line 15     newlist.append (item) = 'buzz' SyntaxError: can not assign to function call

Any idea what I can do? At the end of the program I need to list the integers and strings.

Thanks!

    
asked by anonymous 17.10.2016 / 15:48

2 answers

0
  

SyntaxError: can not assign to function call

This error is happening because you are trying to assign a value in a function call.

Ex:

newlist.append(item) ='fizzbuzz'

You are trying to assign a value in the append function.

As mentioned by @TNT, you have to change your syntax.

intList = list()
intList = [1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]
newlist=list()

def fizzbuzz( lista ):
   for item in lista:
       if item % 3 == 0 and item % 5 == 0:
           lista.remove(item)
           newlist.append('fizzbuzz')
       elif  item % 3 == 0:
           lista.remove(item)
           newlist.append('fizz')
       elif  item % 5 == 0:
           lista.remove(item)
           newlist.append('buzz')
       else:
           newlist.append(item)

fizzbuzz(intList)

print(newlist)

Functional Example
link

    
17.10.2016 / 16:02
0

I have no Python experience, but I believe the 4 append calls are wrong. Try changing

newlist.append(item) ='fizzbuzz'

for

newlist.append('fizzbuzz')
    
17.10.2016 / 15:58