Search code examples
loopswhile-looppython-3.8

My while true loop which is supposed to count to 100 with breaks isn't working


I was supposed to create a while true loop. The program should be counting to 100, printing each number. However, instead of counting the multiples of 3, 5 and 3 and 5 the program has to print a sentence. The loop should exist after printing the number 100. But my program isn't running at all.

I would like for my program to print every number from 1-100, but for multiples of 3, 5 and 3 and 5 it should be pronting a different sentence. For example: multiple of 3 print x, Multiples of 5 print y and multiples of 3 and 5 print xy. It should break on 100 This is the code i have currently written:

count = 1
while True:
  if count % 3 == 0 and count % 5             == 0:
     print("hello there, you gorgeous")
     count = count + 1
     continue
  if count % 3 == 0:
     print("hello world")
     count = count + 1
     continue
  if count % 5 == 0:
     print("love yourself")
     count = count + 1
     continue
  if count > 100:
     break
     print(count)
     count = count + 1

Solution

  • There are a few issues:

    • The program starts with count is 1, which means that none of the if conditions will be true. In that case count does not increase and so the loop will repeat in just the same state as before, leading to an infinite loop. You should increment count also when none of the conditions is true. This is bug probably caused by an indentation error for the last two statements (which follow the break). These should be executed unconditionally and be unindented.

    • The instructions say that you should print each number, but your code is not doing that: even after fixing the indentation problem, the continue statements will make that the print call is skipped. This problem could be solved by placing the print at the top of the loop body.

    • The exit condition should be tested always, not only in the case you don't have to print a phrase. For this reason, it is better to move this test to the top of the loop's body.

    Here is your code with minimal corrections. Comments indicate where the above points were corrected:

    count = 1
    while True:
      if count > 100:  # this condition must be tested in each iteration
         break
      print(count)   # Always print the number
      if count % 3 == 0 and count % 5 == 0:
         print("hello there, you gorgeous")
         count = count + 1
         continue
      if count % 3 == 0:
         print("hello world")
         count = count + 1
         continue
      if count % 5 == 0:
         print("love yourself")
         count = count + 1
         continue
      count = count + 1  # indentation issue solved
    

    This solves the issues, but:

    • it has some code repetition:

      • count = count + 1: To avoid this duplication, don't use continue here, and use elif statements instead -- so to avoid that execution enters multiple conditional blocks.
      • count % 3 == 0: To avoid this duplication, use a different test for the first case: count % 15 == 0 will be true if, and only when, a number is a multiple of both 3 and of 5.
    • it would probably be nice to print the phrases next to the numbers they apply to, not below them. You can use the end parameter with the print call.

    • You could use count += 1 instead of count = count + 1

    • Indenting first with 2 spaces, and then with 3 spaces is inconsistent. A common habit is to use 4 spaces always.

    count = 1
    while True:
        if count > 100:
            break
        print(count, end=" ")   # print phrase on the same line as number
        # use if-elif to avoid duplication
        if count % 15 == 0:  # avoid two modulo operations here
            print("hello there, you gorgeous")
        elif count % 3 == 0:
            print("hello world")
        elif count % 5 == 0:
            print("love yourself")
        else:
            print()
        count += 1
    

    Finally, although I understand you were asked to use while True, this is not the right practice for this scenario. This is the ideal candidate for a range based loop:

    for count in range(1, 101):
        print(count, end=" ")   # print phrase on the same line as number
        # use if-elif to avoid duplication
        if count % 15 == 0:  # avoid two modulo operations here
            print("hello there, you gorgeous")
        elif count % 3 == 0:
            print("hello world")
        elif count % 5 == 0:
            print("love yourself")
        else:
            print()
        count += 1