Question

Write robust python code that take from the user the name of file to open. Then,...

Write robust python code that take from the user the name of file to open.

Then, take name from the user to search inside the file then display its phone number if found

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

def fetch_phone_number():
    # Enter the file name which you wish to open
    filename = input("Enter the filename  to be opened")
    dict = {}
    try:
        # Open the file in read mode
        with open(filename, 'r') as fp:
            # read the file line by line
            line = fp.readline()
            while line:
                line = line.split()

                # storeall the usernames with phone numbers in a dictionary
                dict.update({line[0]: line[1]})
                line = fp.readline()

            # If user exist print the phone number
            # otherwise handle the exception
            user_name = input("Enter the user_name to  be searched")
            if dict.get(user_name) is not None:
                print(dict.get(user_name))
            else:
                print("User does not exist")

    except FileNotFoundError:
        print("File does not exist")


fetch_phone_number()

Add a comment
Know the answer?
Add Answer to:
Write robust python code that take from the user the name of file to open. Then,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT