-4

Hi why my program isn't working ?

 elif begin == 4:
    firstname = input('Enter a firstname:')
    lastname = input('Enter a lastname:')
    phone = input('Enter a phone number:')
    imp = (f'\'{firstname}\'\'{lastname}\'\'{phone}\'')
    
    
    
    with open('Phonebook','r') as firstfile, open('Reception','a') as secondfile:
        
            if lastname and firstname and phone in firstfile: 
                
                secondfile.write(imp)
                print('phonebook exported!')
            else:
                print('This phonebook doesn\'t exist !')

It always return the else and idk why

enter image description here

What is it ? ( I did this after applying the answers )

My file is not closed why it displays this

n004rp
  • 45
  • 5

1 Answers1

1

There are several problems here.

First, Your if statement probably doesn't do what you think it does.

You've written:

            if lastname and firstname and phone in firstfile: 

That statement is more accurately written like this:

            if (lastname) and (firstname) and (phone in firstfile): 

That is, you are treating lastname and firstname as boolean values, which means they will always evaluate to True when non-empty, and then you're checking if phone in firstfile.

This is your second problem: you can't use in to check if a string exists in a file; that condition will always return False. You need to read the file content into a variable, giving you something like:

    with open('Phonebook','r') as firstfile, open('Reception','a') as secondfile:
        
            # read the file into a string
            firstcontent = firstfile.read()

            # now we can check if `<some_string_var> in firstcontent`
            if lastname in firstcontent and firstname in firstcontent and phone in firstcontent: 
                
                secondfile.write(imp)
                print('phonebook exported!')
            else:
                print('This phonebook doesn\'t exist !')

You could simplify that condition using the all builtin:

if all(x in firstcontent for x in [firstname, lastname, phone]):

Here's a demonstration of it working. Assuming that I have a file "Phonebook" that contains the following content:

bob example 123-456-7890

And this code:

firstname = input("Enter a firstname:")
lastname = input("Enter a lastname:")
phone = input("Enter a phone number:")
imp = f"'{firstname}''{lastname}''{phone}'"

with open("Phonebook", "r") as firstfile, open("Reception", "a") as secondfile:

    # read the file into a string
    firstcontent = firstfile.read()

    # now we can check if `<some_string_var> in firstcontent`

    if all(x in firstcontent for x in [firstname, lastname, phone]):
        secondfile.write(imp)
        print("phonebook exported!")
    else:
        print("This phonebook doesn't exist !")

If I provide responses that match an entry in the phonebook:

$ python phonebook.py
Enter a firstname:bob
Enter a lastname:example
Enter a phone number:123-456-7890
phonebook exported!
$ cat Reception
'alice''example''123''bob''example''123-456-7890'

Whereas if I provide a response that doesn't match:

$ python phonebook.py
Enter a firstname:alice
Enter a lastname:example
Enter a phone number:987-654-3210
This phonebook doesn't exist !
larsks
  • 277,717
  • 41
  • 399
  • 399