[Tutor] Why is this error showing up? (Original Message: (Tutor) What's wrong with this code?) Ignore previous post.

Ewald Ertl ewald.ertl at hartter.com
Thu Jul 7 09:36:53 CEST 2005


Hi!


on Thu, 7 Jul 2005 01:13:48 -0600  "Nathan Pinno" <falcon3166 at hotmail.com> wrote :
---------------------------------------------------------------------------------------------

Nathan Pinno >   Thanks Wolfram for help with that error.
Nathan Pinno > 
Nathan Pinno >   Here's another that popped up:
Nathan Pinno > 
Nathan Pinno >   Traceback (most recent call last):
Nathan Pinno >     File "D:\password.py", line 68, in ?
Nathan Pinno >       for x in site.keys():
Nathan Pinno >   AttributeError: 'str' object has no attribute 'keys'
Nathan Pinno > 

The Traceback tells you on which line the error is. 
site comes from your input : site = raw_input("Site: ")
which is a string and not a dictionary.



Nathan Pinno >   How to fix it?
Nathan Pinno > 
Nathan Pinno >   Thanks,
Nathan Pinno >   Nathan Pinno
Nathan Pinno >   ----- Original Message ----- 
Nathan Pinno >   From: "Wolfram Kraus" <kraus at hagen-partner.de>
Nathan Pinno >   To: <tutor at python.org>
Nathan Pinno >   Sent: Thursday, July 07, 2005 1:02 AM
Nathan Pinno >   Subject: Re: [Tutor] Why is this error showing up? (Original Message: 
Nathan Pinno > (Tutor) What's wrong with this code?) Ignore previous post.
Nathan Pinno > 
Nathan Pinno > 
Nathan Pinno >   > You wrote filename == raw_input("Filename to load: ") instead of
Nathan Pinno >   > filename = raw_input("Filename to load: ")
Nathan Pinno >   >
Nathan Pinno >   > HTH,
Nathan Pinno >   > Wolfram
Nathan Pinno >   >
Nathan Pinno >   > Nathan Pinno wrote:
Nathan Pinno >   >> Hi all,
Nathan Pinno >   >>
Nathan Pinno >   >> Here's one of the messages that pops up:
Nathan Pinno >   >>
Nathan Pinno >   >> Traceback (most recent call last):
Nathan Pinno >   >>   File "D:\password.py", line 77, in ?
Nathan Pinno >   >>     filename == raw_input("Filename to load: ")
Nathan Pinno >   >> NameError: name 'filename' is not defined
Nathan Pinno >   >>
Nathan Pinno >   >> Why is it popping up whenever I try to load a file?
Nathan Pinno >   >>
Nathan Pinno >   >> Here's the latest code:
Nathan Pinno >   >>
Nathan Pinno >   >> # This is the code for a password protected program to store passwords.
Nathan Pinno >   >> password = "hello"
Nathan Pinno >   >> print "The Password Program"
Nathan Pinno >   >> print "Copyright 2005 Nathan Pinno."
Nathan Pinno >   >> print
Nathan Pinno >   >> answer = raw_input("What is the password? ")
Nathan Pinno >   >> while password != answer:
Nathan Pinno >   >>     print "The password is incorrect."
Nathan Pinno >   >>     answer = raw_input("What is the password? ")
Nathan Pinno >   >> def main_menu():
Nathan Pinno >   >>     print "1) Add a login info card"
Nathan Pinno >   >>     print "2) Lookup a login info card"
Nathan Pinno >   >>     print "3) Remove a login info card"
Nathan Pinno >   >>     print "4) Print Login info list"
Nathan Pinno >   >>     print "5) Save login list"
Nathan Pinno >   >>     print "6) Open Login list"
Nathan Pinno >   >>     print "9) Exit"
Nathan Pinno >   >>
Nathan Pinno >   >> def load_login(site,filename):
Nathan Pinno >   >>     in_file = open(filename,"r")
Nathan Pinno >   >>     while 1:
Nathan Pinno >   >>         in_line = in_file.readline()
Nathan Pinno >   >>         if len(in_file) == 0:
Nathan Pinno >   >>             break
Nathan Pinno >   >>         in_line = in_line[:-1]
Nathan Pinno >   >>         [site,id,passcard] = string.split(in_line,",")
Nathan Pinno >   >>         list[site] = id and passcard
Nathan Pinno >   >>     in_file.close()
Nathan Pinno >   >>
Nathan Pinno >   >> def save_login(site,filename):
Nathan Pinno >   >>     out_file = open(filename,"w")
Nathan Pinno >   >>     for x in site.keys():

site comes form your input :          site = raw_input("Site: ")
and is a string and not a dictionary. 
Perhaps you will use here "sites", but there are no items added to 
sites??


Nathan Pinno >   >>         out_file.write(x+","+sites[x]+"\n")
Nathan Pinno >   >>     out_file.close()
Nathan Pinno >   >>
Nathan Pinno >   >> menu_choice = "0"
Nathan Pinno >   >> list = {}
Nathan Pinno >   >> print "Welcome to the second half of the program."
Nathan Pinno >   >> print main_menu()
Nathan Pinno >   >> while menu_choice != "9":
Nathan Pinno >   >>     menu_choice = raw_input("Choose an option: ")
Nathan Pinno >   >>     if menu_choice == "1":
Nathan Pinno >   >>         print "Add a login info card"
Nathan Pinno >   >>         site = raw_input("Site: ")
Nathan Pinno >   >>         id = raw_input("User ID: ")
Nathan Pinno >   >>         passcard = raw_input("Password: ")
Nathan Pinno >   >>         list[site] = id and passcard
Nathan Pinno >   >>         menu_choice = raw_input("Choose an option: ")
Nathan Pinno >   >>     elif menu_choice == "2":
Nathan Pinno >   >>         print "Lookup a login info card"
Nathan Pinno >   >>         site = raw_input("Site: ")
Nathan Pinno >   >>         if site.has_key(site):
Nathan Pinno >   >>             print "The ID is: ",id(site)
Nathan Pinno >   >>             print "The password is: ",passcard(site)
Nathan Pinno >   >>         else:
Nathan Pinno >   >>             print site," was not found."
Nathan Pinno >   >>         menu_choice = raw_input("Choose an option: ")
Nathan Pinno >   >>     elif menu_choice == "3":
Nathan Pinno >   >>         print "Remove a login info card"
Nathan Pinno >   >>         site = raw_input("Site: ")
Nathan Pinno >   >>         if sites.has_key(site):
Nathan Pinno >   >>             del numbers[site]
Nathan Pinno >   >>         else:
Nathan Pinno >   >>             print site," was not found."
Nathan Pinno >   >>         menu_choice = raw_input("Choose an option: ")
Nathan Pinno >   >>     elif menu_choice == "4":
Nathan Pinno >   >>         print "Login Info"
Nathan Pinno >   >>         for x in site.keys():

Here's the next use of the input-String "site" as a dictionary which will fail. 



Nathan Pinno >   >>             print "Site: ",x," \tID: ",numbers[x]," \tPassword: 
Nathan Pinno > ",numbers[x]
Nathan Pinno >   >>         print
Nathan Pinno >   >>         menu_choice = raw_input("Choose an option: ")
Nathan Pinno >   >>     elif menu_choice == "5":
Nathan Pinno >   >>         filename = raw_input("Filename to save: ")
Nathan Pinno >   >>         save_login(list,filename)
Nathan Pinno >   >>         menu_choice = raw_input("Choose an option: ")
Nathan Pinno >   >>     elif menu_choice == "6":
Nathan Pinno >   >>         filename == raw_input("Filename to load: ")
Nathan Pinno >   >>         load_login(list,filename)
Nathan Pinno >   >>         menu_choice = raw_input("Choose an option: ")
Nathan Pinno >   >> print "Have a nice day!"
Nathan Pinno >   >>
Nathan Pinno >   >> Anything else that needs addressing?
Nathan Pinno >   >>
Nathan Pinno >   >> Thanks,
Nathan Pinno >   >> Nathan Pinno
Nathan Pinno >   >> http://www.npinnowebsite.ca/
Nathan Pinno >   >>
Nathan Pinno >   >
Nathan Pinno >   > _______________________________________________
Nathan Pinno >   > Tutor maillist  -  Tutor at python.org
Nathan Pinno >   > http://mail.python.org/mailman/listinfo/tutor
Nathan Pinno >   >
Nathan Pinno > _______________________________________________
Nathan Pinno > Tutor maillist  -  Tutor at python.org
Nathan Pinno > http://mail.python.org/mailman/listinfo/tutor
Nathan Pinno > 


------------------- end ----------------------

HTH Ewald



More information about the Tutor mailing list