[Tutor] Why does invalid syntax pop up? (fwd)

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sat Jul 9 00:14:44 CEST 2005


[Nathan, please don't send only to me: make sure you're using "Reply to
All" and that tutor at python.org is also being replied to.  I'm actually
going slightly crazy with work right now, but there are other people on
the mailing list who can help.

I'm forwarding your message to the rest of the mailing list now.  Good
luck!]


---------- Forwarded message ----------
Date: Fri, 8 Jul 2005 16:08:53 -0600
From: Nathan Pinno <falcon3166 at hotmail.com>
To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] Why does invalid syntax pop up?

  Here's another:

  Traceback (most recent call last):
    File "D:\password.py", line 73, in ?
      add_login_command()
    File "D:\password.py", line 41, in add_login_command
      sitelist[site] = [id,passcard]
  NameError: global name 'sitelist' is not defined

  And current code:

  # This is the code for a password protected program to store passwords.
  password = "hello"
  print "The Password Program"
  print "Copyright 2005 Nathan Pinno."
  print
  answer = raw_input("What is the password? ")
  while password != answer:
      print "The password is incorrect."
      answer = raw_input("What is the password? ")
  def main_menu_command():
      print "1) Add a login info card"
      print "2) Lookup a login info card"
      print "3) Remove a login info card"
      print "4) Print Login info list"
      print "5) Save login list"
      print "6) Open Login list"
      print "9) Exit"

  def load_login_command(site,filename):
      in_file = open(filename,"r")
      while 1:
          in_line = in_file.readline()
          if len(in_file) == 0:
              break
          in_line = in_line[:-1]
          [site,id,passcard] = string.split(in_line,",")
          list[site] = id and passcard
      in_file.close()

  def save_login_command(site,filename):
      out_file = open(filename,"w")
      for x in site.keys():
          out_file.write(x+","+sites[x]+"\n")
      out_file.close()

  def add_login_command():
      print "Add a login info card"
      site = raw_input("Site: ")
      id = raw_input("User ID: ")
      passcard = raw_input("Password: ")
      sitelist[site] = [id,passcard]

  def lookup_login_command():
      print "Lookup a login info card"
      site = raw_input("Site: ")
      if sitelist.has_key(site):
          print "The ID is: ",sitelist[site][0]
          print "The password is: ",sitelist[site][1]
      else:
          print site," was not found."

  def remove_login_command():
      print "Remove a login info card"
      site = raw_input("Site: ")
      if sites.has_key(site):
          del sitelist[site]
      else:
          print site," was not found."

  def display_login_command():
      print "Login Info"
      for x in site.keys():
          print "Site: ",sitelist," \tID: ",sitelist[site]," \tPassword:
",sitelist[site]
      print

  menu_choice = "0"
  list = {}
  print "Welcome to the second half of the program."
  main_menu_command()
  while menu_choice != "9":
      menu_choice = raw_input("Choose an option: ")
      if menu_choice == "1":
          add_login_command()
      elif menu_choice == "2":
          lookup_login_command()
      elif menu_choice == "3":
          remove_login_command()
      elif menu_choice == "4":
          display_login_command()
      elif menu_choice == "5":
          filename = raw_input("Filename to save: ")
          save_login_command()
      elif menu_choice == "6":
          filename = raw_input("Filename to load: ")
          load_login_command()
  print "Have a nice day!"

  Thanks for the input so far,
  Nathan
  ----- Original Message -----
  From: "Danny Yoo" <dyoo at hkn.eecs.berkeley.edu>
  To: "Nathan Pinno" <falcon3166 at hotmail.com>
  Cc: "Tutor" <tutor at python.org>
  Sent: Friday, July 08, 2005 2:48 PM
  Subject: Re: [Tutor] Why does invalid syntax pop up?


  >
  >>   Error Message:
  >>   Traceback (most recent call last):
  >>     File "D:\password.py", line 73, in ?
  >>       add_login_command()
  >>   TypeError: add_login_command() takes exactly 2 arguments (0 given)
  >>
  >>   How do I fix it so that it runs properly, and any other errors that
have
  >>   to be fixed?
  >
  >
  > Hi Nathan,
  >
  > Ok, so the error message is saying "I'm trying to call the
  > add_login_command, but as add_login_command is defined, it needs two
  > arguments.  Isn't this weird?"
  >
  >
  > Let's look at the definition of add_login_command():
  >
  > ######
  > def add_login_command(site,filename):
  >    print "Add a login info card"
  >    site = raw_input("Site: ")
  >    id = raw_input("User ID: ")
  >    passcard = raw_input("Password: ")
  >    sitelist[site] = [id,passcard]
  > ######
  >
  > It's not clear to me why 'site' and 'filename' are in the argument list,
  > as neither are being used as arguments, and no matter what we pass into
  > add_login_command, it looks like the function just ignores whatever it
  > sees.
  >
  >
  > There are two possible ways of fixing this:
  >
  >    1.  When calling add_login_command(), just pass random garbage for
  >    those two parameters:
  >
  >        add_login_command("foo", "bar")
  >
  >    2.  Correct add_login_command() so that it doesn't take in two
  >    parameters.
  >
  >
  > I do not like option one.  *grin*  I strongly recommend fixing the
  > parameter list for add_login_command() so that it doesn't ask for
  > parameters that it never uses.
  >
  >
  > Best of wishes!
  >
  >



More information about the Tutor mailing list