[Tutor] What's wrong with this code?
Nathan Pinno
falcon3166 at hotmail.com
Thu Jul 7 08:44:18 CEST 2005
Hi all,
I meant to ask why the main part after the password is not working right.
No one has answered that yet. When I run the code and try to load a file
that has been saved, a TypeError appears. How do I fix the code so no more
errors will show up. Here is the newest code so far:
# This is the code for a password protected program to store passwords.
password = "hello"
print "The Password Program"
print "Copywrite 2005. All Rights Reserved."
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():
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(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(site,filename):
out_file = open(filename,"w")
for x in site.keys():
out_file.write(x+","+sites[x]+"\n")
out_file.close()
menu_choice = 0
list = {}
print "Welcome to the second half of the program."
print main_menu()
while menu_choice != 9:
menu_choice = 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: ")
list[site] = id and passcard
menu_choice = input("Choose an option: ")
elif menu_choice == 2:
print "Lookup a login info card"
site = raw_input("Site: ")
if site.has_key(site):
print "The ID is: ",id(site)
print "The password is: ",passcard(site)
else:
print site," was not found."
menu_choice = input("Choose an option: ")
elif menu_choice == 3:
print "Remove a login info card"
site = raw_input("Site: ")
if sites.has_key(site):
del numbers[site]
else:
print site," was not found."
menu_choice = input("Choose an option: ")
elif menu_choice == 4:
print "Login Info"
for x in site.keys():
print "Site: ",x," \tID: ",numbers[x]," \tPassword:
",numbers[x]
print
menu_choice = input("Choose an option: ")
elif menu_choice == 5:
filename = raw_input("Filename to save: ")
save_login(list,filename)
menu_choice = input("Choose an option: ")
elif menu_choice == 6:
filename == raw_input("Filename to load: ")
load_login(list,filename)
menu_choice = input("Choose an option: ")
print "Have a nice day!"
Thanks for the input so far,
Nathan Pinno
----- Original Message -----
From: "Brian van den Broek" <bvande at po-box.mcgill.ca>
Cc: <tutor at python.org>
Sent: Tuesday, July 05, 2005 1:03 AM
Subject: Re: [Tutor] What's wrong with this code?
> Andre Engels said unto the world upon 05/07/2005 02:44:
>>>From the program::
>>
>> answer = raw_input("What is the password? ")
>> while password != answer:
>> print "The password is incorrect."
>
> <snip Andre's description of the problem with the above OP's code>
>
>> I think you intended to make it so that
>> the program kept asking for passwords until the right one was given.
>> This is done with:
>> answer = raw_input("What is the password? ")
>> while password != answer:
>> print "The password is incorrect."
>> answer = raw_input("What is the password? ")
>
>
> A small thing, but I think that is better as:
>
> while True:
> answer = raw_input("What is the password? ")
> if password == answer:
> break
> print "The password is incorrect."
>
> It probably runs a bit slower, but who cares, as the bottleneck is in
> the chair, not the chip. The advantage is that there is only one
> statement of the prompt to alter if you wanted to change it later.
>
> But, I think this will be one where reasonable people can differ.
> Andre's version does make the semantics of the loop somewhat more
obvious.
>
> Best to all,
>
> Brian vdB
>
> <snip>
>
>> Andre Engels
>
> <snip OP's question>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list