mercredi 4 septembre 2019

print strings from list

I am intended to construct a list of strings from list of numbers. For example, the given list is

list=[1,2,5,25,6]

Desired output:

['Odd', 'Even', Odd, 'multiples of 5 and odd', 'multiples of 2 and even']

My work so far:

list=[]
for num in numbers:
        if num % 2:
            list.append('Odd')
            if not num % 5:
               list.append('multiples of 5 and odd')
            else:
               if not num % 5:
                 list.append('multiples of 2 and even')
               else: 
                 list.append('even')
print(list)  

It printed the list but in a wrong way. I was wondering you if you could review my code. Thanks for your help!

Aucun commentaire:

Enregistrer un commentaire