Python代码出现Unresolved reference错误,求原因及解决方法
Hey there! Let's work through the issues in your code, starting with that confusing Unresolved reference error and fixing other bugs that'll keep your calculator from working right.
First: The Unresolved Reference Error
This is likely popping up because you're trying to modify global variables (like cards, ace, queen) inside your function without telling Python they're global. By default, if you assign to a variable inside a function, Python treats it as a local variable. When you try to read it first (like in your print("Cards = ", cards) line) before assigning, it throws an error because the local version doesn't exist yet. To fix this, you need to declare all global variables at the top of your function.
Other Critical Bugs
Let's go through the other issues breaking your code:
- Mixing up
=(assignment) and==(comparison): Lines likequeen == queen - 1are checking if queen equals queen minus 1 (which is always false) instead of updating the queen count. You need to use=or-=for assignment. - String vs. number mismatch: Your variables like
acehold numbers (e.g.,ace = 4), butinput()returns a string. Comparing a string like"ace"to the number4will never match—so all valid inputs end up in theelse: print("Error")block. - Unfinished code branches: You left logic out for jack, king, two-three-eight, etc.—those card counts won't update when drawn.
- Incorrect update for
nine:nine == -1is totally wrong; you want to subtract 1 from the current nine count, not compare it to -1.
Fixed Code Example
Here's a cleaned-up version of your code with these issues addressed:
# Initialize card counts total_cards = 52 card_counts = { "ace": 4, "queen": 4, "king": 4, "jack": 4, "two": 4, "three": 4, "four": 4, "five": 4, "six": 4, "seven": 4, "eight": 4, "nine": 4, "ten": 4 } def ask_what_card_was_drawn(): # Declare global variables we need to modify global total_cards, card_counts drawn_card = input("What card did you draw? (e.g., ace, ten): ").lower() # Check if the input is a valid card if drawn_card in card_counts: # Update counts card_counts[drawn_card] -= 1 total_cards -= 1 print("Card count updated!") else: print("Error: That's not a valid card name.") # Print remaining cards print("\nCards left:") print(f"Total cards remaining: {total_cards}") for card, count in card_counts.items(): print(f" {card}: {count}") # Optional: Ask for another draw another = input("\nDraw another card? (y/n): ").lower() if another == "y": ask_what_card_was_drawn() # Start the program ask_what_card_was_drawn()
Key Improvements in This Version
- Used a dictionary (
card_counts) to store card values instead of 13 separate variables—this eliminates repetitive code and makes updates easier. - Added
.lower()to the input so users can type "Ace", "ACE", etc., and it still matches. - Added a check for valid card names using
in card_counts. - Included an optional prompt to draw another card (you can remove this if you don't want it).
- Fixed the global variable declaration so updates work correctly.
内容的提问来源于stack exchange,提问作者The CesarDUDE




