Skip to content
Advertisement

I have a problem with if statement and numbers

my problem is I want to make the elif statement work on numbers only but the elif statement work on any datatype, is there any way i can separate the numbers in string type from the other data types

The Code

```address_book = {'1111': 'Amal', '2222': 'Mohammed', '3333': "Khadijah", '4444': 'Abdullah'}


def enter():
    i = input('Please Enter The number: ')
    if len(i) != 4:
        print('This is invalid number')
    elif i in list(address_book):
        print(address_book[i])
    elif i in list(address_book.values()):
        print(list(address_book.keys())[list(address_book.values()).index(i)])
    **elif i.isdigit() not in list(address_book.keys()):
        print('Sorry, the number is not found')
        m = input('do you want to add new number? ')
        if m == 'yes':
            d = input('Please Enter The new number: ')
            w = input('Please Enter the number owner name: ')
            address_book[d] = w
            f = input('Do yo want to see address book: ')
            if f == 'yes':
                print(address_book)
            else:
                pass**
        else:
            pass
    else:
        print('This is invalid number')


enter()```

OutPut

```Please Enter The number: sdaa
Sorry, the number is not found
do you want to add new number? yes
Please Enter The new number: 3333
Please Enter the number owner name: Waleed
Do yo want to see address book: yes
{'1111': 'Amal', '2222': 'Mohammed', '3333': 'Waleed', '4444': 'Abdullah'}

Process finished with exit code 0```

Advertisement

Answer

You can use and for this:

elif i.isdigit() and i not in address_book:

Note that you can use in to check if a key in the dictionary, so you don’t need to call .keys() or convert it to a list for that.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement