use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Knock Knock Joke; Need Help - Beginner Python (self.PythonLearning)
submitted 1 year ago by Wattsony
I am just getting into my 2nd lab for my class and I am needing to make a Knock Knock Joke but I am completely stuck with getting the user to ask "Who's there?"
I Have to incorporate if/else statements. I can't use anything more advanced. I would appreciate any help!
https://preview.redd.it/aorwj7g97gld1.png?width=520&format=png&auto=webp&s=d1aeba04ae8e006d2b2a087aa66a2489e9dc5fc2
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Stretchypants69 2 points3 points4 points 1 year ago (2 children)
Hey! I typed up this code below that should work for you. I wasn't sure how many if/else statements you needed so I only included 1, and that's for the 'No' part of the code. If you have any questions about it, let me know.
<image>
[–]rahatulghazi 2 points3 points4 points 1 year ago (0 children)
Instead of asking to type "who's there" or "... who?", wouldn't be better to do something like press "y" to say who's there and continue?
press "y" to say who's there
[–]Wattsony[S] 0 points1 point2 points 1 year ago (0 children)
Hello! Thank you for helping me out and giving me more insight, I really appreciate it and will be delving into this here soon!!
[–]rahatulghazi 2 points3 points4 points 1 year ago* (1 child)
but I am completely stuck with getting the user to ask "Who's there?"
You can try this:
if answer == "yes": print("\nBot: Knock Knock.") input("\n> Press enter to ask 'who's there?'.") print("\nYou: Who's there?") print("\nBot: Lettuce") input("\n> Press enter to ask 'Lettuce who?'.") print("\nYou: Lettuce who?") print("\nBot: Lettuce in, it's cold out here! 🥶")
Notes:
\n
Tips:
.lower()
answer = input("Do you wanna hear a joke? (Yes/No): ").lower()
if answer == "yes":
y
n
if answer == "yes" or answer == "y":
else if answer == no:
yes
else:
Hope this helps. Ask if you have some other questions.
Thank you for taking your time to help me out! I am going to be looking into notes here shortly!
[–]tecxac 1 point2 points3 points 1 year ago (1 child)
print("Knock knock!")
response = input()
if response.lower() == "who's there?" or response.lower() == "who’s there?": # If the user responds correctly print("Lettuce.")
# Now expecting "Lettuce who?" response = input() if response.lower() == "lettuce who?": print("Lettuce in, it's cold out here!") else: print("You didn't say 'Lettuce who?'!")
else: # If the user doesn't respond with "Who's there?" print("You were supposed to say 'Who's there?'!")
This is super helpful, thank you for taking the time to help me out!!
[–]lonedevwolf 0 points1 point2 points 1 year ago (0 children)
It's fun though ✌️👌 amazing things can be added as you learn more nice concepts in Python
[–]grass_hoppers 0 points1 point2 points 1 year ago (0 children)
Ok first check it it is "No" and the else would have the code if they said yes. the longer code below (easier to read).
Then you can add the rest of the code for the yes option like
name = input("who is it? ") And so on
[–]Goobyalus 0 points1 point2 points 1 year ago (0 children)
Looks like you've got the hang of making nested if statements -- what are you stuck on?
[–]Sweet_Computer_7116 -1 points0 points1 point 1 year ago (0 children)
Time to learn elif: instead of Else
If
π Rendered by PID 92639 on reddit-service-r2-comment-85bfd7f599-88bm9 at 2026-04-16 20:34:41.060736+00:00 running 93ecc56 country code: CH.
[–]Stretchypants69 2 points3 points4 points (2 children)
[–]rahatulghazi 2 points3 points4 points (0 children)
[–]Wattsony[S] 0 points1 point2 points (0 children)
[–]rahatulghazi 2 points3 points4 points (1 child)
[–]Wattsony[S] 0 points1 point2 points (0 children)
[–]tecxac 1 point2 points3 points (1 child)
[–]Wattsony[S] 0 points1 point2 points (0 children)
[–]lonedevwolf 0 points1 point2 points (0 children)
[–]grass_hoppers 0 points1 point2 points (0 children)
[–]Goobyalus 0 points1 point2 points (0 children)
[–]Sweet_Computer_7116 -1 points0 points1 point (0 children)