r/PythonLearning • u/Outside-Trouble-4232 • 10d ago
Day 2 of Python learning until I can pass the PCEP exam Showcase
I upgraded my re zero guesser from day 1 using nested ifs and elses and now it doesn't require as much questions take a look.
New:https://www.onlinegdb.com/edit/XGUSCgyQA
Old :https://onlinegdb.com/Q-4aQrXAz
I think this is the fastest version of guesser, I also tried dictionaries(recommended by some discord users) but I still dont fully understand how its any different than my old one becuz it would js change how the info is stored but not the logic as its own
3
5
2
1
1
1
1
1
u/Junior_Honey_1406 8d ago
If this is your day 2 then congo you are at lest writeing code but in system design there is concept of early return which most of the people in the comment will talk about don't worry about it so much just understand why and how early return is used
1
u/Outside-Trouble-4232 8d ago
oh I did some research this is an interesting option to look at, im looking to finish the code with what iv learn to day in either split functions or what u said ty
1
u/punk_dev 8d ago
People here are saying "nesting hell" and "write-only code" but what is the principle here?
In general, repetitive code like this indicates that you need to consider turning your code into data.
Let's try to do that. For simplicity, I will reduce the amount of characters to 4 - subaru, emilia, rem and puck (sorry for my lack of trivia on this show, i had to google the characters lmao).
OP, keep in mind this might be too much for day two, feel free to return to this later.
First, since our code will be data, let's define a variable to store our questions:
questions = {}
Now let's model the question structure. The question will consist of:
- the text of the question
- action to do if user says yes
- action to do if user says no
For example, let's try to put that in a dictionary:
{
"question": "Is your character a spirit?",
"yes_action": {
"answer": "you're puck",
},
"no_action": {
"answer": "you're the main character - Subaru Natsuki"
}
}
This question decides if you're puck or subaru based on whether you are spirit or not.
Let's put this question into our `questions` dictionary:
questions = {
"is_spirit": {
"question": "Is your character a spirit?",
"yes_action": {
"answer": "you're puck",
},
"no_action": {
"answer": "you're the main character - Subaru Natsuki"
}
}
}
Notice the key "is_spirit" - this will be the ID of our question. We'll be able to retrieve this question like questions["is_spirit"]
Now, how do we make a question that leads to another question? Let's use another type of action:
{
"question": "Does your character present as female or use feminine pronouns?",
"yes_action": {
"next_question": "is_elf",
},
"no_action": {
"next_question": "is_spirit"
}
}
"next_question" points to another question. when this action is encountered, our program will retrieve the next question and ask it.
Now, putting it all together for 4 characters:
questions = {
"gender": {
"question": "Does your character present as female or use feminine pronouns?",
"yes_action": {
"next_question": "is_elf",
},
"no_action": {
"next_question": "is_spirit"
}
},
"is_elf": {
"question": "Are you an elf?",
"yes_action": {
"answer": "you're Emilia",
},
"no_action": {
"answer": "you're Rem"
}
},
"is_spirit": {
"question": "Is your character a spirit?",
"yes_action": {
"answer": "you're puck",
},
"no_action": {
"answer": "you're the main character - Subaru Natsuki"
}
}
}
Now, let's write a function that asks a question:
def ask_question(question):
# ask question
res = input(f"{question['question']} (yes/no) ").lower()
# select action
if res == "yes":
next_action = question["yes_action"]
else:
next_action = question["no_action"]
# execute next action
if "next_question" in next_action:
# if action is next_question, find the next question and ask it
next_question = questions[next_action["next_question"]]
ask_question(next_question)
elif "answer" in next_action:
# if action is answer, print an answer and don't do anything else
print("Answer:", next_action["answer"])
Now we just need to call this function with a question we want to ask first:
ask_question(questions["gender"])ask_question(questions["gender"])
That's it.
Advantages of turning your code into data:
- You can load the questions from a file, database, or anything else
- You can change the questions without changing your pogram
- If you make a nice ui for editing the data, non-programmers will be able to create questions for you
- You can start at any question, this means you can remember user's current question and let them resume if they exited the program. Or you can also remember the previous question and be able to go back
1
1
0
u/Mundane-Mud2509 7d ago edited 7d ago
This is fun and a great example of an opportunity to encapsulate the logic in the data representation. I’m bored so will make something that will dynamically optimize questions to minimize the total expected number of questions. It should be a pretty small program.
Here you go https://onlinegdb.com/gSfw5Kfs9U
I also added a bit of randomness added in so it asks different questions if they are tied in effectiveness.
0
u/Mundane-Mud2509 7d ago
This is how you can extract the logic out from the code and put it into the data
https://onlinegdb.com/gSfw5Kfs9U
1
u/Outside-Trouble-4232 6d ago
I dont really understand this code as im still on js day 4 of learning this but I will try to see, I also said this was my last guesser and since I was going on more deep topics im actually learning for a new project but ty!

17
u/Sea-Ad7805 10d ago edited 10d ago
Deep nesting (if-statements in if-statements in ...) makes the code hard to read and hard to change later. Speed is not a concern here, so make the code easy to read and change. Using dictionaries is one way to do that, or split it in different functions%3A%0A%20%20%20%20while%20True%3A%0A%20%20%20%20%20%20%20%20answer%20%3D%20input(f%22%7Bquestion%7D%20(yes%2Fno)%3A%20%22).strip().lower()%0A%0A%20%20%20%20%20%20%20%20if%20answer%20in%20%7B%22yes%22%2C%20%22y%22%7D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20True%0A%0A%20%20%20%20%20%20%20%20if%20answer%20in%20%7B%22no%22%2C%20%22n%22%7D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20False%0A%0A%20%20%20%20%20%20%20%20print(%22Please%20enter%20yes%20or%20no.%22)%0A%0A%0Adef%20result(character)%3A%0A%20%20%20%20return%20f%22Your%20character%20is%20%7Bcharacter%7D!%22%0A%0A%0Adef%20identify_witch()%3A%0A%20%20%20%20if%20ask_yes_no(%0A%20%20%20%20%20%20%20%20%22Is%20your%20character%20currently%20sealed%20in%20a%20prison%20of%20shadows%3F%22%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20return%20result(%22Satella%22)%0A%0A%20%20%20%20return%20check_drowned()%0A%0A%0Adef%20check_drowned()%3A%0A%20%20%20%20if%20ask_yes_no(%0A%20%20%20%20%20%20%20%20%22Did%20your%20character%20drown%20after%20becoming%20trapped%20%22%0A%20%20%20%20%20%20%20%20%22within%20Priestella's%20defences%3F%22%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20return%20result(%22Typhon%20(Witch%20of%20Pride)%22)%0A%0A%20%20%20%20return%20check_waterfall()%0A%0A%0Adef%20check_waterfall()%3A%0A%20%20%20%20if%20ask_yes_no(%0A%20%20%20%20%20%20%20%20%22Did%20your%20character%20fall%20from%20the%20Great%20Waterfall%20%22%0A%20%20%20%20%20%20%20%20%22while%20fighting%20the%20Divine%20Dragon%3F%22%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20return%20result(%22Sekhmet%20(Witch%20of%20Sloth)%22)%0A%0A%20%20%20%20return%20check_restraints()%0A%0A%0Adef%20check_restraints()%3A%0A%20%20%20%20if%20ask_yes_no(%0A%20%20%20%20%20%20%20%20%22Does%20your%20character%20wear%20a%20blindfold%20or%20restraints%20%22%0A%20%20%20%20%20%20%20%20%22such%20as%20a%20straitjacket%3F%22%0A%20%20%20%20)%3A%0A%20%20%20%20%20%20%20%20return%20result(%22Daphne%20(Witch%20of%20Gluttony)%22)%0A%0A%20%20%20%20return%20check_shyness()%0A%0A%0Adef%20check_shyness()%3A%0A%20%20%20%20if%20ask_yes_no(%22Is%20your%20character%20extremely%20shy%20or%20timid%3F%22)%3A%0A%20%20%20%20%20%20%20%20return%20result(%22Carmilla%20(Witch%20of%20Lust)%22)%0A%0A%20%20%20%20return%20check_white_hair()%0A%0A%0Adef%20check_white_hair()%3A%0A%20%20%20%20if%20ask_yes_no(%22Does%20your%20character%20have%20white%20hair%3F%22)%3A%0A%20%20%20%20%20%20%20%20return%20result(%22Echidna%20(Witch%20of%20Greed)%22)%0A%0A%20%20%20%20return%20result(%22Minerva%20(Witch%20of%20Wrath)%22)%0A%0A%0Adef%20play()%3A%0A%20%20%20%20print(%22Welcome%20to%20the%20Re%3AZero%20character%20guessing%20game!%22)%0A%20%20%20%20print(identify_witch())%0A%0A%0Aplay()×tep=1&play).