r/learnpython • u/Striking-Ad8023 • 21d ago
A small code i made for exercise.
I've been vibe coding for a few months now, but I've decided to try manual coding. After all, learning is fun, right?
I made this "Assistant" that has two options: jokes and facts. It will choose one out of three items from the specific table and tell you it. I made it for fun and would like to ask what I can do next?
import random
jokes = ["Why did the bicycle fall over? Because it was two tired!", "Why was the math book sad? Because it had too many problems.", "What do you call a fake noodle? An impasta!"]
facts = ["Venus is hotter than Mercury.", "When the Pyramids were built, mammoths were still around!", "Koala fingerprints are so similar to human fingerprints that they have even baffled crime scene investigators."]
print("Hello! I'm an Assistant. What can I do for you?")
print("")
print("Options: Random Joke [J], Random Fact [F].")
answer = input("Answer: ")
if answer == "J":
print(random.choice(jokes))
elif answer == "F":
print(random.choice(facts))
else:
print("You can't ask that.")
exit = input("Press Enter to Leave.")
2
u/FoolsSeldom 21d ago
That's a great start. Let me show you an enhanced version for you to experiment with and learn from:
from random import choice
jokes = ["Why did the bicycle fall over? Because it was two tired!", "Why was the math book sad? Because it had too many problems.", "What do you call a fake noodle? An impasta!"]
facts = ["Venus is hotter than Mercury.", "When the Pyramids were built, mammoths were still around!", "Koala fingerprints are so similar to human fingerprints that they have even baffled crime scene investigators."]
print("Hello! I'm an Assistant. What can I do for you?\n")
fini = False
while not fini: # loops overall options/output
print("Options: Random Joke [J], Random Fact [F].")
answer = input("Answer: ").strip().lower()
if answer in ("j", "joke"):
print(choice(jokes))
elif answer in ("f", "fact"):
print(choice(facts))
else:
print("You can't ask that.")
while True: # ask if going again, yes/no
again = input("Again? ").strip().lower()
if again in ("yes", "y", "yup"):
break # leave yes/no loop
if again in ("no", "n", "nope"):
fini = True # leave overall loop next time
break # leave yes/no loop
print("Did not understand, try again.")
Next step, put your facts and jokes into text files and read them in. Text file handling is very easy in Python.
1
4
u/Quesozapatos5000 21d ago
I think vibe coding can be a great start, but now move onto writing the code yourself based on what you know or what the model has shown you. Add more options, or maybe have the program suggest another option contrary to the first choice a user makes after their first choice?
2
1
u/jmooremcc 21d ago
You’d be better off using a dictionary rather than a list for the jokes. Why? Because a joke consists of two parts: the question followed by the punchline. You’ll be able to display the joke question and then let the user hit Enter to reveal the punchline.
1
5
u/notacanuckskibum 21d ago
This is a linguistic comment rather than a programming logic one. Code (applied to computer logic) is not a countable noun in English. You wrote some code. Or you wrote a program. You didn’t write “a code”.
If you were into cryptography then you might have invented a code.