Respuesta :
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++;
}
}
}
}
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\n"))
if pas=="quit":
print("You quit!")
break
num=int(input("Please Enter the Number\n"))
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!

