Member Avatar for leegeorg07

hi again sorry i havent been only lately but i have this problem with some code for a digital phonebook. the code is this:

p = float(raw_input("do you want to add a new person? (1 = yes, 0 = no)"))
v = float(raw_input ("do you want to see the phonebook now? (1 = yes, 0 = no)")
n = raw_input("what is the persons name?")
nb = raw_input("what is there number?")
close = print "thank you for using a George Lee program"
raw_input("press enter to close")

phonebook = {'Andrew':8806336, \
             'Emily':6784346, \
             'Peter':7658344, \
             'Lewis':1122345 }
print phonebook

raw_input("press enter to show how to add a key and a value")

phonebook['Ginger' ] = 1234567

print phonebook

print p

while p == 1:
    print n
    print nb
    phonebook[a] = b
    print v
        while v == 1:
          print phonebook
    while v == 0:
          print close
while p == 0:
    print close

print "thank you for using a George Lee program"
raw_input("press enter to close")

when i try to run this it says that the 'n' syntax is invalid
how could i fix this?

also on a different note how could i modify the code to 'save' any additions i make to this dictionary e.g. adding another person to it?

can you post the error message that your compiler gave you? It
may at least point you/us to a line where the error is instead of having to look through your code for it.

You're missing a parenthesis at the end of the v = float(raw_input( line.

Also close = print "thank you for using a George Lee program" is incorrect syntax. You can either print that string or save it using close = . Which is it?

Ok, so i found a couple of syntactical errors and fixed them in the code...

p = float(raw_input("do you want to add a new person? (1 = yes, 0 = no)"))
v = float(raw_input ("do you want to see the phonebook now? (1 = yes, 0 = no)"))
n = raw_input("what is the persons name?")
nb = raw_input("what is there number?")
close = "thank you for using a George Lee program"
raw_input("press enter to close")

But you have an error at:
phonebook[a] = b
because you have not yet defined b.

Member Avatar for leegeorg07

thanks ive fixed those but now the error says that theres an invalid syntax for n = raw_input("what is the persons name?")

You have to realize that 9 times out of 10 when you have a syntax error it's actually at the line above. So if you peer at the line above n = raw_input("what is the persons name") you'll notice that you're missing a trailing parenthesis, as I mentioned in my last post.

There are 7 syntax errors that I found in your code. 4 of which are infinite loops.

Member Avatar for leegeorg07

thanks i didnt notice that but now i have a problem

because whenever i run the edited code:

phonebook = {'Andrew':8806336, \
             'Emily':6784346, \
             'Peter':7658344, \
             'Lewis':1122345 }
print phonebook

raw_input("press enter to show how to add a key and a value")

phonebook['Ginger' ] = 1234567

print phonebook

p = float(raw_input("do you want to add a new person? (1 = yes, 0 = no)"))

while p == 1:
    n = raw_input("what is the persons name?")
    nb = raw_input("what is there number?")
    phonebook[n] = nb
    v = float(raw_input("do you want to see the phonebook now? (1 = yes, 0 = no)"))
    while v == 1:
          print phonebook
    while v == 0:
          close = "thank you for using a George Lee program"
          raw_input("press enter to close")

while p == 0:
    close = "thank you for using a George Lee program"
raw_input("press enter to close")

print "thank you for using a George Lee program"
raw_input("press enter to close")

the phonebook after someone has been added just keeps showing how can i stop this?

By getting rid of your infinite loops (ie, while p == 0: . You need to realize that since p is never changed within that loop, it will continuously run. I assume that you really want an if statement. Here is an example of how you would use an if:

>>> a = 0
>>> if a == 1:
...     print 'The value of a is a 1!'
... elif a == 2:    
...     print 'The value of a is a 2!'
... else:    
...     print 'The value of a is neither 1, nor 2.  It is', a
...     
The value of a is neither 1, nor 2.  It is 0
>>>

Here is a better way to handle flow of control :)

def youWantToAddAPerson():
  return int(raw_input("Do you want to add a new person (0 or 1) ? "))

def getThePersonName():
  return raw_input("What is the name of the person ? ")

def getThePersonNumber():
  return raw_input("What is the number of that person ? ")

def youWantToSeeTheNotebook():
  return int(raw_input("Do you want to see the notebook now (0 or 1) ? "))

def displayTheNotebook():
  print "not yet implemented"

def addThePersonToNotebook(name, number):
  # XXX not yet implemented
  pass

def thankUserAndQuit():
  print "Thank you for using Gribouilli's software"


while youWantToAddAPerson():
  name = getThePersonName()
  number = getThePersonNumber()
  addThePersonToNotebook(name, number)
  if youWantToSeeTheNotebook():
    displayTheNotebook()

thankUserAndQuit()
Member Avatar for leegeorg07

thanks Gribouilli but i have to say that at the moment jlm699's answer is easier however as i improve my programming skills i will look into that but thank you anyway

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.