Day 061 - Project 61 : Twitter rip off

You can change the tabbing type of a repl under ‘Settings’ in that repl.

2 Likes

So i’m on day 61 of the 100 days of code course and have just begun working with db, but for some reason when I add something to the database it isn’t added in the correct order like at all there’s not even a pattern to it. And I don’t know why because on the solution video the inputs are added to the database in chronological order. Please help my code is below.

from replit import db
import datetime, os

while True:
  choice = input("""Do you want to:
1. Add a tweet
2.View tweets\n""")
  os.system("clear")
  if choice == "1":
    tweet = input("What would you like to tweet?\n")

    time = datetime.datetime.now()
    db[time] = tweet
    os.system("clear")
  elif choice == "2":
    keys = db.keys()
    count = 0

    for key in keys:
      print(key, db[key])
      count += 1
      print(count)
      if count % 10 == 0:
        carryon = input("Do you want to see another 10 tweets?\n")
        os.system("clear")

        if carryon == "no":
          break
        elif carryon == "yes":
          continue

Next time, please use code fences for all of your code.

Now to answer your question, dictionaries in pythob don’t store any order (and replit’s db acts as a dict). To maintain order data, consider putting your tweets in a list.

I will next time I’ve never used this before so that’s why I didn’t.

And thankyou I’ll try that.

2 Likes

If it works, consider marking my above answer as the solution so that other community members with a similar issue can easily find the problem.

Also just a point on your solution can I make the database work with lists or would I have to use files.
And how come David’s(tutorial guy) was ordered when he just used the database.

Just to clarify by ordered I mean like if you add something it goes in the first or last spot depending.

You can put a list in Replit DB.

db['key']=['a','b','c']
3 Likes

How do you make the tweets show in reverse chronological order?
Here is my code:

from replit import db
import datetime, os, time

running = True


def add():
  tweet = input("What is your tweet? ")
  timestamp = str(datetime.datetime.now())
  db[timestamp] = tweet
  time.sleep(1)
  os.system("clear")


def view():
  counter = 0
  keys = db.keys()
  for key in keys:
    print(f"{key}: {db[key]}")
    counter += 1
    if counter == 10:#This is the part I'm not getting
      exit = input("Do you want to load another 10 tweets? ")
      if exit == "yes":
        continue
      else:
        time.sleep(1)
        os.system("clear")


while running:
  menu = int(input("1: Add A Tweet\n2: View Tweets\n> "))
  if menu == 1:
    add()
  elif menu == 2:
    view()

Question:
I don’t understand David’s solution because he’s using code that wasn’t covered in previous lessons (which he does way too often imo).

So how do I solve this using my code? I’m having an issue with the reverse.sort() function. I just can’t get this to work.

Or if my code is just bad, can someone help me understand what David is doing with [::-1] ?

Tutorial number: Day 61

Repl link: https://replit.com/@tyler58/day-61100days?v=1#main.py

from replit import db
import datetime
import os
import time

def addTweet():
  tweet = input("What do you want to tweet?\n")
  timestamp = datetime.datetime.now()
  key = f"mes{timestamp}"
  db[key] = tweet
  time.sleep(2)
  os.system("clear")

def viewTweet():
  keys = db.keys()
  tweetList = [(key, db[key]) for key in db.keys()]
  tweetList.sort(reverse=True)
  for key in keys[:10]:
    print(f"""{key}: {db[key]}""")

def twitter():
  while True:
    menu = input("Add or View tweets.\n").capitalize()
    if menu == "Add":
      addTweet()
      continue
    elif menu == "View":
      viewTweet()
      continue
    else:
      print("That's not an option. Try again.")
      continue
    time.sleep(2)
    os.system("clear")
  
twitter()