Day 061 - Project 61 : Twitter rip off

If you have any questions, comments or issues with this project please post them here!

Hi everyone,

I was having a problem with day 61. I managed to view tweets in order, 5 at a time as I wanted, but when user inputs they want to stop viewing messages, it causes an infinite loop. Can anyone explain why?

Below you see I have for loop inside a while True loop. Once user should have caused a break inside the for loop: system seemed to ignore break command, ignore counters, and just print till eternity. I tried modifying my IFs and counters and later I made it into a function, but I only beat this problem once I removed the while True loop :hot_face:
Why infinite loop begins after break command was meant to kill the print?
If interested, you should see final working program here.

from replit import db
import os, time, datetime


while True:
    os.system("clear")
    choice = input("Do you want to \nAdd a tweet (1) \nSee old tweets (2):\n> ").strip()

    if choice == "1":
        tweet = input("Write your tweet: ")
        timestamp = datetime.datetime.now()
        db[timestamp] = tweet

    elif choice == "2":
        keys = db.keys()
        keys = sorted(keys)
        
        print()
        
        counter = 0
        counter2 = 0

        while True:
            for key in list(reversed(keys)):
                print(db[key])
                print()
                counter += 1
                counter2 += 1

                if counter == 5:
                    print()
                    see_more = input("More tweets? y/n > ").strip().lower()
                    print()
                    if see_more[0] == "y":
                        continue
                    else:
                        break

            if counter2 == len(list(keys)):
                print("<<System notice: that is all, folks!>>")
                break

        time.sleep(2)
        
    else:
        print()
        print("You must press 1 or 2.")
        time.sleep(1)
        continue

Hi @Monco_Carser thanks for your question.

The break command only stops execution of the current loop (in this case, the “for key in list” loop). The while loop still continues.

3 Likes