Python函数choose_website语法错误排查求助(基础问题)
Hey there! Let's fix up your code—there are a few syntax issues and best practice tweaks to get it working as you want. Here's a breakdown of the problems and the corrected version:
Issues in your original code:
- Case-sensitive keyword mistake: Python requires
if(lowercase) instead ofIf—it's case-sensitive, so the capital I throws a syntax error. - Wrong operator for comparison: You used
=(assignment) instead of==(equality check) in your conditionals. You can't assign values inside anif/elifcheck, so this will break the code. - Unclosed parenthesis: When calling
choose_website, you didn't close the parenthesis forraw_input()—that's a simple syntax oversight. - Global variable vs return value: Your function tries to assign to a global
websitevariable, but without declaringglobal websiteinside the function, it'll create a local variable instead. Even worse, the function doesn't return anything, so you won't get the URL back when you call it. - Print statement (Python 3 note): If you're using Python 3,
printis a function and needs parentheses around its argument (your code uses Python 2 syntax here).
Corrected Code (cleaner, maintainable version):
Instead of a long chain of if-elif statements, using a dictionary to map site names to URLs is better practice—it's easier to update later. Here's how that looks:
def choose_website(website_name): # Map site names to their URLs in a dictionary site_urls = { "nfl": "https://www.nfl.com", "nba": "http://www.nba.com", "fifa": "http://www.fifa.com" } # Check if the input matches a valid site if website_name in site_urls: return site_urls[website_name] else: print("Invalid website, please try again") return None # Return None to signal invalid input # Get user input and use the function user_choice = input("Please insert website(nfl/nba/fifa): ") selected_url = choose_website(user_choice) # Only print the URL if it's valid if selected_url: print(f"Your selected URL: {selected_url}")
If you prefer sticking with the if-elif structure:
Here's the fixed version using your original approach:
def choose_website(website_name): nfl = "https://www.nfl.com" nba = "http://www.nba.com" fifa = "http://www.fifa.com" if website_name == "nfl": return nfl elif website_name == "nba": return nba elif website_name == "fifa": return fifa else: print("Invalid website, please try again") return None user_choice = input("Please insert website(nfl/nba/fifa): ") selected_url = choose_website(user_choice) if selected_url: print(f"Your selected URL: {selected_url}")
Key fixes here:
- Swapped
Ifforif - Changed
=to==in conditionals - Added closing parenthesis when calling the function
- Removed the global
websitevariable and instead return the URL directly (better practice than relying on globals) - Updated
raw_inputtoinput()(for Python 3 compatibility; useraw_inputif you're on Python 2) - Added parentheses to
printstatements for Python 3 - Added return values so the function gives you the URL when you call it
内容的提问来源于stack exchange,提问作者Alon
相关产品推荐
相关产品推荐

