This program is supposed to mimic a phone book. This program also needs to be menu driven. The general idea is to accept a first name, last name, or phone number from the user and to read through a TEXT file,looking for a match. If you look at my program and what I have so far, it is easy to see that the text file location is ...C:\python26\ entries.txt and can be easily madeup. If you were to open the text file it would reas as follows: jones\n tom\n 208-203-3450\n johnson\n rob\n 345-324-1234\n and so on...
I think this program should work, but for some reason i keep getting a " TypeError: 'function' object is not iterable which is keeping my program from accesing the text document..
any suggestions? - ##call to input file and return
-
##also calls to "lookup" modules for output
-
-
def main():
-
myLastName = []
-
myFirtName = []
-
myPhoneNumber = []
-
lname,fname,phone = getInput
-
-
choice = 0
-
-
##menu options
-
while choice !='4':
-
choice = raw_input('1.lookup by last name\n 2.lookup by first name\n 3.lookup by phone number\n 4.quit')
-
if choice == '1':
-
getLname(lname, fname, phone)
-
if choice == '2':
-
getFname (lname, fname, phone)
-
if choice == '3':
-
getPhone (lname, fname, phone)
-
if int(choice) < 0 :
-
print "invalid option"
-
if int (choice) > 4:
-
print "invalid option"
-
-
-
def getInput():
-
myfile= "c:\\python26\\entries.txt"
-
fileInput = open(myFile, "r")
-
count = 0
-
for mystring in fileInput:
-
myString = myString.strip()
-
myString = myString.lower()
-
myNum = count % 3
-
if myNum == 0:
-
myLastName.append(myString)
-
elif myNum == 1:
-
myFirstname.append(myString)
-
elif myNum == 2:
-
myPhoneNumber.append(myString)
-
count = count + 1
-
return
-
fileInput.close
-
-
-
-
def getLname(lname, fname, phone):
-
name = raw_input("last name to lookup:").strip().lower()
-
-
pointer = 0
-
if name in lname:
-
while True:
-
try:
-
pointer = lname.index(name, pointer)
-
print fname[pointer].title(),
-
print lame[pointer].title()
-
print phone[pointer]
-
except:
-
break
-
else:
-
print "no entry found for" + name.title()
-
-
def getFname(lname, fname, phone):
-
name = raw_input("first name to lookup:").strip().lower()
-
-
pointer = 0
-
if name in fname:
-
while True:
-
try:
-
pointer = fname.index(name, pointer)
-
print fname[pointer].title(),
-
print lame[pointer].title()
-
print phone[pointer]
-
except:
-
break
-
else:
-
print "no entry found for" + name.title()
-
-
def getPhone(lname, fname, phone):
-
name = raw_input("phone number to lookup:").strip().lower()
-
-
pointer = 0
-
if name in phone:
-
while True:
-
try:
-
pointer = phone.index(name, pointer)
-
print fname[pointer].title(),
-
print lame[pointer].title()
-
print phone[pointer]
-
except:
-
break
-
else:
-
print "no entry found for" + name.title()
-
-
main()
-
-
3 38909 bvdet 2,851
Recognized Expert Moderator Specialist
This line: - lname,fname,phone = getInput
should be: - lname,fname,phone = getInput()
The error message gives it away. The parentheses tell Python to call the preceding function name.
ya that was it..
thanks!!
do you know how i could catch invalid user input on the menu that I have?? I have it programmed to catch any numbers out of range " < 0 and >4.. but it doesn't catch chars.
bvdet 2,851
Recognized Expert Moderator Specialist
You could do something like this: - >>> while True:
-
... option = raw_input("Enter 1,2,3 or 4")
-
... print option
-
... if option in ['1', '2', '3', '4']:
-
... break
-
... else:
-
... print "Invalid option"
-
...
-
6
-
Invalid option
-
A
-
Invalid option
-
2
-
>>>
Use an if/elif block instead of several if blocks. Example: - >>> if option == "2":
-
... print "2"
-
... elif option == "3":
-
... print "3"
-
...
-
2
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Petri Savolainen |
last post by:
After reading the manuals and googling around a bit, I thought I'd use
the 'compile' built-in to create a code object. Then, using either
new.function() or types.FunctionType(), create a function object out of
the code object. The function object can then be turned into a method
for example using types.MethodType(). Right? Well, on Windows 98, using
python 2.2.2 (or 2.3b2):
>>> c=compile('def a(msg): return msg','<nowhere>','exec')
>>>...
|
by: Atul Kshirsagar |
last post by:
Hello,
I am using Python 2.3.2 with a C++ extention DLL in
muti-threaded environment.
1. For each new thread I create a separate
sub-interpreter.
2. Each thread executes multiple python statements
calling the class objects in my extention DLL.
3. Before each execution I import my extention module
using;
|
by: Balaji |
last post by:
Hello Everybody...
I have a problem..
This is the code...
--------------------------------------
class Stack:
def __init__(self,expr):
self.stackP=
|
by: Randall Parker |
last post by:
Using Python 2.4.2 on Windows 2000 in SPE.
Getting:
TypeError: 'str' object is not callable
on this line:
TmpErrMsg1 = "State machine %s " (StateMachineName)
In Winpdb 1.0.6 the StateMachineName is of type str in the Namespace |
|
by: Gary Wessle |
last post by:
dear python users
I am not sure why I am getting
****************************************************************
Traceback (most recent call last):
File "my.py", line 3, in ?
urlparse('http://www.cwi.nl:80/%7Eguido/Python.html')
TypeError: 'module' object is not callable
****************************************************************
| |
by: k.retheesh |
last post by:
Can anybody tell me why am I getting this error message while trying to
print a part of a string. Is there a better approach for this...
Traceback (most recent call last):
File
"C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "Q:\PythonScripts\InventoryCompareCase.py", line 276, in ?
main()
|
by: AWasilenko |
last post by:
I'm trying to test a few different approaches to displaying pages via
Cherrypy and I'm not having much luck. Here is my code so far:
import sys, cherrypy, html
class Root:
@cherrypy.expose
def index(self, pageid = None):
selection = html.Page()
return selection.input()
|
by: Aaron Brady |
last post by:
>>f.func_defaults
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
V. interesting. Operation succeeds but with a throw. Er, raise.
|
by: christophertidy |
last post by:
Hi
I am new to Python and have recieved this error message when trying to
instantiate an object from a class from another file within the same
directory and wondered what I have done wrong.
I have a Step.py class:
class Step(object)
def __init__(self, sName):
"Initialise a new Step instance"
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |