174k views
3 votes
Write a program that reads a string (password) and a number. The program will keep asking the user for a password and number as long as the user does not provide the expected values ("milkyway" and 1847, respectively), and as long as the user has not exhausted the maximum number of attempts . In addition, the program will stop asking for data if the word "quit" is provided as a password value (in this case, the program will not read the number). The program will print the message "Wrong credentials" when an invalid password or number (or both) is provided. If the user eventually provides the correct credentials the message "Access Granted" will be displayed; otherwise the message "Access Denied" will be generated. The following example illustrates the messages to use to read data and to display results.

Enter password: milkyway
Enter number: 10
Wrong credentials
Enter password: tomato
Enter number: 1847
Wrong credentials
Enter password: milkyway
Enter number: 1847
Access Granted

User Virgiliu
by
4.8k points

2 Answers

4 votes

Step-by-step explanation:

The correct password and number are defined and stored in their respective variables. While loop is used to keep on taking input from the user until the user inputs correct password and number or user runs out of attempts. The number of attempts is set to 3 but can be changed to any number as you like.

The user is asked to input password first then we check if the user wants to quit? if true terminate program else continue to get the input number from the user.

Now we have password and number from the user, check if both are correct? if yes then grant the access else wrong credentials add one to the attempt, display the number of attempts left and repeat the whole loop again.

Python Code:

password = "milkyway"

number = 1847

attempts=0

while attempts < 3:

pas=str(input("Please Enter the Password\\"))

if pas=="quit":

print("You quit!")

break

num=int(input("Please Enter the Number\\"))

if pas==password and num==number:

print("Access Granted")

else:

print("Wrong credentials")

attempts+=1

print("Attempts left: ",3-attempts)

Output:

Test 1:

Please Enter the Password

waysnf

Please Enter the Number

1847

Wrong credentials

Attempts left: 2

Please Enter the Password

milkyway

Please Enter the Number

1942

Wrong credentials

Attempts left: 1

Please Enter the Password

milkyway

Please Enter the Number

1847

Access Granted

Test 2:

Please Enter the Password

menas

Please Enter the Number

4235

Wrong credentials

Attempts left: 2

Please Enter the Password

quit

You quit!

Note: feel free to ask in comments if you dont understand anything!

Write a program that reads a string (password) and a number. The program will keep-example-1
Write a program that reads a string (password) and a number. The program will keep-example-2
User Peter Lucas
by
5.0k points
3 votes

Answer:

// Program is written in Java Programming Language

// Comments are used for explanatory purpose

// Program starts here

import java.util.Scanner;

public class checkPass{

public static void main (String [] args)

{

// Call scanner function

Scanner input = new Scanner (System.in);

// Declare user password and number

string userpass;

int usernum;

// Initialise password and number

string password = "milkway";

int number = 1847;

// Initialise number of attempts to 0

int attempts = 0;

while (attempts<=10 )

{

// Prompt user to enter password and number

System.out.println("Enter password: ");

userpass = input.nextLine();

System.out.println("Enter Number: ");

usernum = input.nextInt();

if(userpass == password && usernum == number)

{

System.out.print("Access Granted");

attempts = 11;

}

else

{

System.out.print("Wrong Credentials");

attempts++;

}

}

}

}

User RaptorFactor
by
5.0k points