7-1 Please login 分数 10 作者 马杨珲 单位 浙江科技学院 Design a login program to allow users to enter password. (1)If the password entered is zust, print Success. This indicates that the login is complete (2)Otherwise print Password error, and the user can enter again. The user is allowed to enter at most 3 times. After three failures, print Login failed. Input Specification: Enter the password in one line. If there is an error, you need to re-enter it. At most 3 times. Output Specification: Print results according to inputshell 编程
时间: 2024-03-18 10:38:54 浏览: 39
Sure, here's a possible solution in shell:
```shell
#!/bin/bash
password="zust"
tries=0
while [ $tries -lt 3 ]
do
read -p "Please enter password: " input_password
if [ "$input_password" == "$password" ]
then
echo "Success"
exit 0
else
echo "Password error"
tries=$((tries+1))
fi
done
echo "Login failed"
exit 1
```
Explanation:
- We set the correct password to "zust" and initialize the number of tries to 0.
- We use a while loop to allow the user to enter the password at most 3 times.
- Inside the loop, we use the `read` command to read the input password from the user.
- We compare the input password with the correct password using the `==` operator in an `if` statement.
- If the passwords match, we print "Success" and exit the program with status code 0 (which means success).
- If the passwords don't match, we print "Password error" and increment the number of tries.
- At the end of the loop, if the number of tries is 3 or more, we print "Login failed" and exit the program with status code 1 (which means failure).