33 lines
776 B
Python
33 lines
776 B
Python
# simple_joke_menu.py
|
|
import sqlite3
|
|
import random
|
|
|
|
db = sqlite3.connect('jokes.db')
|
|
|
|
while True:
|
|
print("\n1. Get joke")
|
|
print("2. Add joke")
|
|
print("3. Quit")
|
|
|
|
choice = input("Your choice: ")
|
|
|
|
if choice == "1":
|
|
joke = db.execute("SELECT joke FROM jokes ORDER BY RANDOM() LIMIT 1").fetchone()
|
|
if joke:
|
|
print(f"\n🤣 {joke[0]}")
|
|
else:
|
|
print("No jokes yet!")
|
|
|
|
elif choice == "2":
|
|
new = input("Your joke: ")
|
|
if new:
|
|
name = input("Your name: ") or "Friend"
|
|
db.execute("INSERT INTO jokes (joke, contributor) VALUES (?, ?)", (new, name))
|
|
db.commit()
|
|
print("Saved!")
|
|
|
|
elif choice == "3":
|
|
break
|
|
|
|
db.close()
|
|
print("Bye!") |