Home > Blockchain >  I have a problem with elif statement and else statement and dictionary in python
I have a problem with elif statement and else statement and dictionary in python

Time:02-08

The Code

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


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

what I want is that the console print 'This is invalid number' if i have entered any data type in the console except integer but error show up in the console page

OutPut

Please Enter The Number: d
Traceback (most recent call last):
  File "C:\Users\walee\PycharmProjects\pythonProject\main.py", line 19, in <module>
    mo()
  File "C:\Users\walee\PycharmProjects\pythonProject\main.py", line 7, in mo
    r = int(r1)
ValueError: invalid literal for int() with base 10: 'd'

Process finished with exit code 1

first screenshot second screenshot

CodePudding user response:

You can use a try catch block here to catch the errors. You will either want to get a different user input if it is invalid, or just exit the program.

try:
  r = int(r1)
except:
  print("This is invalid number")

CodePudding user response:

Wrap the input statement in a while True loop. Break out of the loop if they enter a number.

while True:
    r1 = input("Enter the number: ")
    if r1.isdigit():
        break
    else:
        print("Invalid number, please try again")

r1 = int(r1)
# ... continue with rest of the code

CodePudding user response:

I would use str.isdigit() to do this. This function returns a boolean which is True when the string is made only out of numbers and False otherwise. Here's the code:

def mo():
    r1 = input('Please Enter The Number: ')
    if not r1.isdigit():
        print("This is invalid number")
    else:
        r = int(r1)
        if r in men:
            print(men[r])
        elif len(str(r)) > 10:
            print('This is invalid number')
        elif len(str(r)) < 10:
            print('This is invalid number')
        elif r not in men:
            print('Sorry, the number is not found')
        else:
            print('This is invalid number')

So, first check whether the input is a number. If it isn't, print that the input is invalid and stop excuting the code, in order to prevent the program from crashing anyway. If it is a number, continue with the rest of the code.

However, I noticed that your code can be simplified into the following:

def mo():
    r1 = input('Please Enter The Number: ')
    if not r1.isdigit() or len(r1)!=10:
        print('This is invalid number')
    else:
        r = int(r1)
        if r in men:
            print(men[r])
        else:
            print('Sorry, the number is not found')

This code works in the following way:

First, it checks whether the input is a number and its length is equal to 10 ( or, its length is not bigger and not smaller than 10). Then it converts the input into a number and checks whether the number is in the dictionnary.

  •  Tags:  
  • Related