[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 12:51:58 CEST 2005


Hello!

I just looked a little over the code, what it is perhaps ment to do now. 

As allready mentioned by Ziyad in your former post:

	just call "main_menu()" 

don't print main_menu()

This calls the funciton main_men(), which prints it menu and 
after this the print-Command in this line print's the return-Value 
of the function : 
e.g. in the python-shell:

>>> def main_menu():
...     print "here in main_menu"
... 
>>> print main_menu()
here in main_menu
None


The next thing is to move main_menu() into the while-Loop, 
so you see the menu every time the loop runs, otherwise it would 
after some inputs disappear from the screen. 
In every if/elif-branch you can delete the last raw_input() requesting 
for an option, because the while-Loop has it's own request at the start of
the loop. 

Here's a snap of the first 2 Options in the menu, I've changed: 
---------
while menu_choice != "9":
    main_menu()
    menu_choice = raw_input("Choose an option: ")
    if menu_choice == "1":
        print "Add a login info card"
        site = raw_input("Site: ")
        id = raw_input("User ID: ")
        passcard = raw_input("Password: ")
        sitelist[site] = [id,passcard]
!!!! Here I asign the data to the dictionary sitelist with the key "site" 
!!!! an list with the value of id as the first entry and the value of passcard
!!!! as the second entry
!!!! I've renamed list to sitelist, because list itself is a builtin creating 
!!!! a list . 
!!!! e.g.:
!!!! >>> a="hallo"
!!!! >>> list(a)
!!!! ['h', 'a', 'l', 'l', 'o']
!!!!

    elif menu_choice == "2":
        print "Lookup a login info card"
        site = raw_input("Site: ")
!!!! as the "list"  is a dictionary we can lookup if the site is 
!!!! available in the sitelist. 
        if sitelist.has_key(site):		
            print "The ID is: ",sitelist[site][0]
!!!! the 0 Element of the list at sitelist[site] is the id and the first 
!!!! one is the password. 

            print "The password is: ",sitelist[site][1]
        else:
            print site," was not found."
---------

I've used a list for storing the elements of id and passcard, but you could also use an other 
dictionary with the keys id and passcard if you like. 

The next problem is in the part of menu_choice =="3" and "4":
There is no dictionary named "numbers". Here you can also use the 
sitelist[site], ... 

I hope that help's you to change the rest of your code. 


Ewald 






More information about the Tutor mailing list