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 input Sample Input #1: abc zust Sample Output #1: Password error Success Sample Input #2: zust Sample Output #2: Success Sample Input #3: china 12345 zuss Sample Output #3: Password error Password error Password error Login failed
时间: 2024-03-23 12:39:18 浏览: 143
Sure, I can help you with that. Here is a sample code in Python:
```python
password = "zust"
for i in range(3):
user_input = input("Please enter password: ")
if user_input == password:
print("Success")
break
else:
print("Password error")
if user_input != password:
print("Login failed")
```
Explanation:
- The variable `password` stores the correct password.
- We use a for loop to allow the user to enter the password at most 3 times.
- Inside the loop, we prompt the user to enter the password using the `input()` function and store it in the variable `user_input`.
- We check if the `user_input` matches the `password`. If it does, we print "Success" and exit the loop using the `break` statement.
- If the `user_input` does not match the `password`, we print "Password error" and continue with the loop.
- If the loop completes without finding a match, we print "Login failed".
阅读全文