lundi 28 juin 2021

If/Else Program wont print index

I am writing a program that will print a list of numbers 1:n. However, every number divisible by 2 should be replaced with 'x' and every number divisible by 3 should be replaced with 'y'. Lastly, every number both divisible by 2 and 3 should be replaced with 'xy'. This is my code so far, I can get the function to print the correct result with my input. But it wont print the entire list of numbers. Instead I am just getting the end result (x,y, or xy) for the given input. Here is my code thus far with the output.

def replace(n):
    
    for i in range(n):
        print(i)

        if n%2== 0 and n%3 == 0:
            return 'xy'

        elif n%2==0:
            return 'y'

        elif n%3 == 0:
            return 'x'

        else: 
            print(i)
replace(12)

This is my output:

0
'xy'

I would like the output to be something like:

1
x
y
x
5
xy
7
x
y
x
11
xy

Any advice would be appreciated, you guys rock!

Aucun commentaire:

Enregistrer un commentaire