基于random.choice与if/elif的答题游戏答案判断异常问题排查
Hey there! Let's figure out why your game is acting wonky and fix it up step by step.
The Core Issues
Your code has two key problems causing the weird behavior:
- You're not saving the random question you show the user: Right now, you print a random card with
print(random.choice(card_list)), but you don't store that specific card in a variable. Then in yourif/elifchecks, you either compare strings to therandom.choicefunction itself (which will never match) or—if you adjusted that later—pull a new random card each time you check. This means the question the user sees isn't the same one your code is verifying against. - Redundant, error-prone condition checks: Writing a separate
eliffor every card makes your code long and easy to mess up with typos or mismatches.
The Fix
First, store the randomly selected card in a variable so you can reuse it both for displaying to the user and checking their answer. Second, use a dictionary to map each card to its correct character—this cleans up your code a ton and avoids messy elif chains.
Here's the corrected code:
import random # Create a dictionary that links each question card to its correct character card_to_person = { 'The main character': 'aleem', 'He brings aleem on holiday': "aleem's dad", 'Dies in the middle of the book': "aleem's mum", "She takes care of aleem while aleem's dad is in Ethiopia": 'Miss Fitzgerald', 'He helps take care of aleem with Miss Fitzgerald': 'Mr Fitzgerald', "He is in charge of aleem's court case": 'Nicolas' } # Pick a random card and store it in a variable (so we use the same one for display and checking) selected_card = random.choice(list(card_to_person.keys())) print(selected_card) # Get the user's answer user_answer = input('Who is this? ') # Check if the answer matches (case-insensitive to handle small typos) if user_answer.strip().lower() == card_to_person[selected_card].lower(): print("correct") else: print(f'wrong! The correct answer is {card_to_person[selected_card]}')
What Changed & Why It Works
- Dictionary mapping: Instead of separate variables for each card and person, we use a single dictionary to link questions to answers. This makes adding/removing questions way simpler later on.
- Stored selected card: We save the random card to
selected_cardso we're always checking against the exact question the user saw—no more random mismatches. - Case-insensitive check: Added
.strip().lower()to both the user's answer and the correct answer so small typos like capitalization or extra spaces don't count as wrong (you can remove this if you want strict exact matches). - Helpful feedback: Tells the user the correct answer when they get it wrong, which makes the quiz more engaging.
Why Your Original Code Failed
In your original code, lines like if 'The main character' == random.choice and choice == person1: are comparing the string 'The main character' to the random.choice function itself, not a selected card string. Since a string can never equal a function, all those conditions would never be true—so it always hit the else and printed 'wrong'. Even if you changed that to random.choice(card_list), you'd be picking a new random card each time you checked a condition, leading to random correct/wrong messages that don't match the user's actual answer.
内容的提问来源于stack exchange,提问作者user10593432

