-1

I am trying to achieve this using case statement

k=[]
for i in range(1,100):
     if i%3 and i%5 == 0:
            i=i,"divides with both"
     elif i%3 ==0:
            i =i,"divides with 3 "
     elif i%5 == 0:
            i = i,"divides with 5"
     else:
            i=i
     k.append(i)
gtlambert
  • 11,711
  • 2
  • 30
  • 48
Harish
  • 157
  • 2
  • 11

1 Answers1

1

My initial response (as a comment above) is that this looks like a homework problem and shouldn't be on StackOverflow.

My second response is that you shouldn't be trying to emulate this style switch statement in Python as it embeds data in the code which is poor programming.

My third response is that achieving this functionality without any 'if' at all in the code is actually a tricky problem. So here's my wacky solution:

CASES = ("divides with both", False, False, "divides with 3", False, "divides with 5", "divides with 3", False, False, "divides with 3", "divides with 5", False, "divides with 3", False, False)

LIMIT = 100

k = []

for n in range (1, LIMIT + 1):
    result = ((n, CASES[n % len(CASES)]), n)

    k.append(result[result[0][1] == False])

print(k)
cdlane
  • 40,441
  • 5
  • 32
  • 81