函数choose_website语法错误(基础知识)

时间:2018-04-01 10:43:09

标签: python python-3.x

我正在尝试编写一个函数来选择多个已保存网址的网站。 我的功能如下:

website = ''
def choose_website(website_name):
  nfl = 'https://www.nfl.com'
  nba = 'http://www.nba.com'
  fifa = 'http://www.fifa.com'
  If website_name = "nfl":
      website = nfl
  elif website_name = "nba":
      website = nba
  elif website_name = "fifa":
      website = fifa
  else:
      print 'Invalid website, please try again'
choose_website(raw_input("Please insert website(nfl/nba/fifa): ")

简而言之,我将网站URLS存储在他们的名字中,我要求用户输入名称,这将在“网站”变量中存储所需的网站URL。 但是当我尝试运行它时,我收到了语法错误:

  If website_name = "nfl":
            ^
SyntaxError: invalid syntax

Process finished with exit code 1

我不知道为什么会这样,有人可以帮助我吗? 我正在使用Python 3.4

1 个答案:

答案 0 :(得分:0)

正如人们所说python is case sensitive. Change If to if 并将=替换为==加倍 value-comparisons

中的更多详细信息
  if website_name == "nfl":
  #^^             ^^
      website = nfl
  elif website_name == "nba":
  #                 ^^
      website = nba
  elif website_name == "fifa":
  #                 ^^
      website = fifa

请阅读pep-8