Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a problrm with if statement and while loop and dictionary in python

men = {1111111111: 'Amal', 2222222222: 'Mohammed', 3333333333: 'Khadijah', 4444444444: 'Abdullah', 5555555555: 'Rawan',
       6666666666: 'Faisal', 7777777777: 'Layla'}


def mo():
    r = int(input('Please Enter The Number: '))
    if r in men:
        print(men[r])
    elif r not in men:
        print('Sorry, the number is not found')
    elif r >= 11:
        while r >= 11:
            r = r + 1 == 1
            print('This is invalid number')
mo()

I have problem in the elif. What I want is if I write more than 10 characters in the console, then the code will print 'This is invalid number'. But instead the code prints 'Sorry, the number is not found'.

Output:

Please Enter The Number: 111111111111111
Sorry, the number is not found

Process finished with exit code 0

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The answer posted by ksohan is correct, but there’s a catch. What you want is if the number entered is greater than 10 characters, then console will print ‘This is invalid number’. If you want to compare the length of characters then you should first typecast r variable to string and then compare its length with 10.

Try this:

men = {1111111111: 'Amal', 2222222222: 'Mohammed', 3333333333: 'Khadijah', 4444444444: 'Abdullah', 5555555555: 'Rawan',
       6666666666: 'Faisal', 7777777777: 'Layla'}


def mo():
    r = int(input('Please Enter The Number: '))
    if r in men:
        print(men[r])
    elif len(str(r)) > 10:
        print('This is invalid number')
    elif r not in men:
        print('Sorry, the number is not found')
mo()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading