Quantcast

Maximum PC

It is currently Sat Dec 20, 2014 4:58 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Python question
PostPosted: Sat Aug 22, 2009 2:01 pm 
8086
8086
User avatar

Joined: Wed Jul 29, 2009 11:45 am
Posts: 5
Hey guys, I'm having a problem with a very simple database test. I need to be able to find the key to a specific value, and check to see if a value exists. This is what I have (I've bolded the parts that I need help on, and put in fake methods that hopefully explain what I need to do with it.:

-edit- Could I just search for the value in a list of the values and then get the key from that? I know I can use this for finding if the value exists, but I'm pressed for time and need to go do something, so I'll edit it when I can. -/edit-

-edit2- O.O The post took all of my formatting! :( -/edit2-

Thanks in advance!

Code:
import pdb
class Basic_Files
   #instantiate main variables
   phonebook = {}
   option = 3
   choice = 1
   new_name = ""
   new_number = ""
   look_up_name = ""
   loop_up_num = ""
   y/n = ""
   loop = 1
   while loop == 1:

      print "Welcome to a basic phone book."
      print "Please choose a number:"
      print "[1] Add a new name"
      print "[2] Find a number"
      print "[3] Close the phonebook"

                pdb.set_trace()
      try:
         option = input(">> ")
      except KeyboardInterrupt, err:
         print "Keyboard input interrupted, Closing,"
         loop = 0
      else:
         if option == 1:
            print "Please enter the new name."
            new_name = input(">> ")
            if phonebook.has_key(new_name):
               print "That entry already exists!"
            else:
               print "Please enter the new number."
               new_number = input(">> ")
               if phonebook.[b]has_value(new_number)[/b]:
                  print "This number already exists!"
               else:
                  phonebook [new_name] = new_number
                  print "Entry complete. Would you like to view it now?"
                  y/n = input("[y/n] >> ")
                  if y/n == "y":
                     print phonebook[new_name]
                  else:
         elif option == 2:
            print "Please choose:"
            print "[1] Find a name"
            print "[2] Find a number"
            try:
               choice = input(">> ")
            except KeyboardInterrupt, err:
               print "Keyboard input interrupted, closing."
               loop = 0
            else:
               if choice == 1:
                  print "Please insert the number:"
                  try:
                     look_up_num = input(">> ")
                  except KeyboardInterrupt, err:
                     print "Keyboard input interrupted, closing."
                     loop = 0
                  else:
                     if phonebook.[b]has_value(look_up_num):[/b]
                        print phonebook.[b]key_of(look_up_num)[/b]
         elif option == 3:
            loop = 0
         else:
            loop = 1


Edit by smartcat99s: added code tags


Top
  Profile  
 
 Post subject:
PostPosted: Sat Aug 22, 2009 9:58 pm 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4745
Location: In the monkey's litterbox
Try getting a list of items by using dictionary.values() and then working from there.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.