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
JavaScript
x
31
31
1
```address_book = {'1111': 'Amal', '2222': 'Mohammed', '3333': "Khadijah", '4444': 'Abdullah'}
2
3
4
def enter():
5
i = input('Please Enter The number: ')
6
if len(i) != 4:
7
print('This is invalid number')
8
elif i in list(address_book):
9
print(address_book[i])
10
elif i in list(address_book.values()):
11
print(list(address_book.keys())[list(address_book.values()).index(i)])
12
**elif i.isdigit() not in list(address_book.keys()):
13
print('Sorry, the number is not found')
14
m = input('do you want to add new number? ')
15
if m == 'yes':
16
d = input('Please Enter The new number: ')
17
w = input('Please Enter the number owner name: ')
18
address_book[d] = w
19
f = input('Do yo want to see address book: ')
20
if f == 'yes':
21
print(address_book)
22
else:
23
pass**
24
else:
25
pass
26
else:
27
print('This is invalid number')
28
29
30
enter()```
31
OutPut
JavaScript
1
10
10
1
```Please Enter The number: sdaa
2
Sorry, the number is not found
3
do you want to add new number? yes
4
Please Enter The new number: 3333
5
Please Enter the number owner name: Waleed
6
Do yo want to see address book: yes
7
{'1111': 'Amal', '2222': 'Mohammed', '3333': 'Waleed', '4444': 'Abdullah'}
8
9
Process finished with exit code 0```
10
Advertisement
Answer
You can use and
for this:
JavaScript
1
2
1
elif i.isdigit() and i not in address_book:
2
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.