You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 of If—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 an if/elif check, so this will break the code.
  • Unclosed parenthesis: When calling choose_website, you didn't close the parenthesis for raw_input()—that's a simple syntax oversight.
  • Global variable vs return value: Your function tries to assign to a global website variable, but without declaring global website inside 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, print is 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 If for if
  • Changed = to == in conditionals
  • Added closing parenthesis when calling the function
  • Removed the global website variable and instead return the URL directly (better practice than relying on globals)
  • Updated raw_input to input() (for Python 3 compatibility; use raw_input if you're on Python 2)
  • Added parentheses to print statements for Python 3
  • Added return values so the function gives you the URL when you call it

内容的提问来源于stack exchange,提问作者Alon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:59:43