实现用户输入用户名和密码,当用户名为admin或administrator且密码为012345时,显示“登录成功”,否则显示“登录失败”,登录失败时允许重复输入三次。Python
时间: 2023-11-30 12:02:02 浏览: 47
可以使用while循环来实现允许重复输入三次,同时用if语句判断用户名和密码是否正确。以下是示例代码:
```python
count = 0
while count < 3:
username = input("请输入用户名:")
password = input("请输入密码:")
if (username == "admin" or username == "administrator") and password == "012345":
print("登录成功")
break
else:
print("登录失败")
count += 1
if count == 3:
print("三次输入均错误,程序结束")
```
在上面的代码中,我们设置了一个计数器`count`,以便最多允许输入三次。当用户输入的用户名和密码都正确时,打印“登录成功”并跳出循环。否则打印“登录失败”,并让计数器加一,直到输入三次为止。最后,如果计数器等于3,则说明已经输入了三次错误的用户名和密码,程序结束。
相关问题
实现用户输入用户名和密码,当用户名为admin或administrator且密码为012345时,显示“登录成功”,否则显示“登录失败”,登录失败时允许重复输入三次。
以下是实现用户输入用户名和密码,当用户名为admin或administrator且密码为012345时,显示“登录成功”,否则显示“登录失败”,登录失败时允许重复输入三次的代码:
```
#include <iostream>
#include <string>
using namespace std;
int main() {
string username, password;
int count = 0;
bool success = false;
while (count < 3 && !success) {
cout << "请输入用户名:";
cin >> username;
cout << "请输入密码:";
cin >> password;
if ((username == "admin" || username == "administrator") && password == "012345") {
cout << "登录成功!" << endl;
success = true;
} else {
cout << "登录失败!" << endl;
count++;
}
}
if (!success) {
cout << "登录失败次数过多,程序退出。" << endl;
}
return 0;
}
```
希望能够帮到你!
请用python编程实现要求:实现用户输入用户名和密码,当用户名为admin或administrator且密码为012345时,显示“登录成功”,否则显示“登录失败”
好的,这个问题可以回答。以下是Python代码实现:
username = input("请输入用户名:")
password = input("请输入密码:")
if (username == "admin" or username == "administrator") and password == "012345":
print("登录成功")
else:
print("登录失败")
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)