0
def six_by_seven(n):
    if n % 42 == 0:
        return "Universe"
    if n % 7 == 0:
        return "Good"
    if n % 6 == 0:
        return "Food"
    else:
        return "Oops"

How would I use a loop in the function stated above to get the output stated down below?

Thanks

1 Oops
2 Oops
3 Oops
4 Oops
5 Oops
6 Food
7 Good
...
41 Oops
42 Universe
...
98 Good
99 Oops
100 Oops

I tried using a for loop with a variable called count. I couldn't figure what to type in for a for loop. I tried a while loop as well. Same problem.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Hamza K
  • 1
  • 3

2 Answers2

1

This was my approach. I used a dictionary so that the code could be changed easily in the future if needed. I also took out the start and end so that the code is not hardcoded.

def six_by_seven():
    start = 1
    end = 100
    outputs = {
        42: "Universe",
        7: "Good", 
        6: "Food"
    }
    for i in range(start, end+1):
        for key in outputs:
            if i % key == 0:
                print(f"{i} {outputs[key]}")
                break
        else:
            print(f"{i} Oops")


if __name__ == '__main__':
    six_by_seven()
Rushil S
  • 78
  • 6
  • Thank you so much Rushii. Can you explain to me what the if __name__ == '__main__': six_by_seven() does? I am confused about that. – Hamza K Mar 26 '23 at 04:18
  • 1
    @HamzaK It's just a good convention. See [this question](https://stackoverflow.com/q/419163). – InSync Mar 26 '23 at 04:25
0

You can either use a for or a while loop to get your desired output.

for i in range(0,101):
  if i % 42 == 0:
    print("do something")
  if ...

or

i = 0

while i <= 100:
  if i % 42 == 0:
    print("do something")
  if ...
  i += 1
David AM
  • 115
  • 10