mercredi 26 décembre 2018

Python lists: Replacing integers with strings. Using if-statements and for-loops

I am trying to use a for-loop to replace integers within a list, with strings. I am trying to use for loops and if statements but it doesn't seem to be working. Is there an easier way to do this? The replace() method only works to replace strings with other strings.

for i in range(len(upto_100)):
    if upto_100[i] % 3 == 0 and upto_100[i] % 5 ==0:
        upto_100[i] == 'FizzBuzz'
    elif upto_100[i] % 3 == 0:
        upto_100[i] == 'Fizz;'
    elif upto_100[i] % 5 == 0:
        upto_100[i] == 'Buzz'
    else:
        upto_100[i] == upto_100[i]

print(upto_100)

Aucun commentaire:

Enregistrer un commentaire