If you have any questions, comments or issues with this project please post them here!
Hey Guys! I have broken some code for you… Can you please fix it?
Haha
Now, seriously, here’s my problem:
I get an interesting ValueError: Circular Reference detected
when trying to store the joke to the replit database.
I know it’s probably because of my functions, but I don’t see, why it shouldn’t work…
Can you please enlighten me?
import requests, json
from replit import db
def joke():
result = requests.get("https://icanhazdadjoke.com/", headers={"Accept":"application/json"})
joke = result.json()["joke"]
id = result.json()["id"]
print(joke)
return joke, id
def save(joke, id):
db[id] = joke
print("Joke saved")
return
def view():
for key in db.keys():
print(f"{key}\n\n")
def menu():
print("The Joke's on You!\n")
select = input("Generate random Joke or view Joke-Database?\n1: Random Joke\n2:View Jokes\n")
if select == "1":
joke()
store = input("Save this Joke?\nYes/No:\t").lower()
if store[0] == "y":
save(joke,id)
if select == "2":
view()
select = input("press enter when done")
return
while True:
menu()
Thanks
You have a var passed to a function called “joke”, and you have a function called “joke”, try renaming one or the other.
Here:
And here:
Hi all
I have an IndentationError: unexpected indent on line 18
. Can you help me fix it.
here is the code:
import requests, json, os, time
from replit import db
while (True):
time.sleep(1)
result = requests.get("https://icanhazdadjoke.com/", headers={"Accept":"application/json"})
joke = result.json()
jk = joke["joke"]
id = joke["id"]
print(jk)
answer = input("\n(s)ave joke, (l)oad old jokes, (n)ew joke\n>").lower()
if answer=="n":
continue
elif answer=="s":
db[id] = jk
print("\nSAVED\n")
continue
else:
keys = db.keys()
for key in keys:
print(db[key])
print("\n")
time.sleep(1)
screenshot:
Thanks
Hi @emer2020, welcome to the community!
An IndentationError
means that you either put too much indentation or not enough. The error specifies line 18, and you’ll notice, the continue
statement was indented too much and not aligned with the print statement. But you’ll also get an error because continue statements cannot be outside of a loop. This is because the rest of your code isn’t indented with the loop. Here’s the fixed code:
import requests, json, os, time
from replit import db
while (True):
time.sleep(1)
result = requests.get("https://icanhazdadjoke.com/", headers={"Accept":"application/json"})
joke = result.json()
jk = joke["joke"]
id = joke["id"]
print(jk)
answer = input("\n(s)ave joke, (l)oad old jokes, (n)ew joke\n>").lower()
if answer=="n":
continue
elif answer=="s":
db[id] = jk
print("\nSAVED\n")
continue
else:
keys = db.keys()
for key in keys:
print(db[key])
print("\n")
time.sleep(1)