English

To secure your account, whether it be an email, online bank account, or any other account, it is important that we use authentication. Use your programming expertise to create a program using - Computer Science (Python)

Advertisements
Advertisements

Question

To secure your account, whether it be an email, online bank account, or any other account, it is important that we use authentication. Use your programming expertise to create a program using a user-defined function named login that accepts userid and password as parameters (login(uid,pwd)) that displays a message “account blocked” in case of three wrong attempts. The login is successful if the user enters the user ID as "ADMIN" and the password as "St0rE@1". On successful login, display the message “login successful”.

Answer in Brief

Solution

Points to consider:

  1. As per the question, user-defined function login(uid, pwd)needs to be created which should display either “Login Successful” or “Account Blocked” after 3 wrong attempts. Therefore, a global variable should be used and its value should increase by 1 after every unsuccessful attempt. 
  2. If the username and password are correct, the program should display “Login Successful” and terminate.
  3. Once the check for the username/password is complete and if the credentials are incorrect, the program should check for the counter value, and if ‘counter’ is more than 2, the program should display “Account Blocked” and exit, otherwise it should give the option to enter the username and password again.

The flow chart for the program can be drawn as follows:

Program:

counter = 0
def logintry():
    ##This function will ask for a username and password from the user and then pass the entered value to the login function
    username = input("Enter username: ")
    password = input("Enter password: ")
    login(username, password);
def login(uid,pwd):
    global counter
    if(uid=="ADMIN" and pwd =="St0rE@1"):
        print("Login Successful")
        return
    #If the username and password are correct the program will exit
    else:
        counter += 1
        print("The username or password is incorrect")
    if(counter>2):
        # check counter value to see
        # the no.of incorrect attempts
        print("Account blocked")
        return
    else:
        # Calling logintry() function to # start the process again
        print("Try again")
        logintry()

# Start the program by calling the function
logintry()

OUTPUT:

When Incorrect values are entered:

Enter username:  user
Enter password: pwd
The username or password is incorrect
Try again

Enter username: uname
Enter password: password
The username or password is incorrect
Try again

Enter username: user
Enter password: password
The username or password is incorrect
Account blocked

When the correct values are entered:

Enter username: ADMIN
Enter password: St0rE@1
Login Successful
shaalaa.com
User Defined Functions
  Is there an error in this question or solution?
Chapter 7: Functions - Exercise [Page 171]

APPEARS IN

NCERT Computer Science [English] Class 11
Chapter 7 Functions
Exercise | Q 1. | Page 171

RELATED QUESTIONS

Which of the following allow the programmer to modu/arize a program.


Write a note on user defined functions.


Write the syntax of functions.


Does a function always return a value? Explain with an example.


Take a look at the series below:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55… To form the pattern, start by writing 1 and 1. Add them together to get 2. Add the last two numbers: 1 + 2 = 3. Continue adding the previous two numbers to find the next number in the series. These numbers make up the famed Fibonacci sequence: the previous two numbers are added to get the immediate new number.


Write a program to check the divisibility of a number by 7 that is passed as a parameter to the user-defined function.


Write a program that uses a user-defined function that accepts name and gender (as M for Male, F for Female) and prefixes Mr/Ms on the basis of gender.


Write a program that has a user-defined function to accept the coefficients of a quadratic equation in variables and calculate its determinant. For example: if the coefficients are stored in the variables a, b, and c then calculate the determinant as b2-4ac. Write the appropriate condition to check determinants on positive, zero, and negative and output appropriate results.


Write a program that implements a user-defined function that accepts Principal Amount, Rate, Time, and Number of Times the interest is compounded to calculate and displays compound interest. (Hint: CI = P*(1 + r/n)nt)


For the SMIS system extended in Chapter 6 let us do the following:

  1. 7.1 Convert all the functionality in Chapters 5 and 6 using user-defined functions.
  2. 7.2 Add another user-defined function to the above menu to check if the student has short attendance or not. The function should accept the total number of working days in a month and check if the student is a defaulter by calculating his or her attendance using the formula: Count of days the student was present or the total number of working days. In case the attendance calculated is less than 78%, the function should return 1 indicating short attendance otherwise the function should return 0 indicating attendance is not short.

Let’s peer review the case studies of others based on the parameters given under “DOCUMENTATION TIPS” at the end of Chapter 5 and provide feedback to them.


Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×