Home > OS >  I have a problem with if statement and numbers
I have a problem with if statement and numbers

Time:02-14

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```

CodePudding user response:

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.

CodePudding user response:

in your dictionary, the key is a string and the default input method takes a string as input, it's simple to take input as int

 i = int( input('Please Enter The number: '))

as the keys are in string format you have to convert to string while checking in elif.

elif str(i) in list(address_book):
elif str(i) in list(address_book.values()):
elif str(i) not in list(address_book.keys()):

full code :

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


def enter():
    i = int( input('Please Enter The number: '))
    if len(str(i)) != 4:
        print('This is invalid number')
    elif str(i) in list(address_book):
        print(address_book[i])
    elif str(i) in list(address_book.values()):
        print(list(address_book.keys())[list(address_book.values()).index(i)])
    elif str(i) 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:
                print(address_book) 
        else:
            pass
    else:
        print('This is invalid number')


enter()
  • Related