一、此次程序需要实现:
1、设定固定的用户名密码
2、用户名密码输入正确打印登录正确信息
3、仅仅运行三次登录
二、本次使用的python版本为:
Windows下版本号:
C:\Users\daisy>python --version
Python 2.7.13rc1
Linux下版本号:
三、设计思路:
1、定义用户名变量:USER_name = "admin1"
2、定义密码变量:PASSWORD = "admin123"
3、由于必须实现3次登录可以采用循环。
四、程序以windows下程序:
以下是在pycharm下编写程序及操作使用raw_input输入:
USER_name = "admin1" #定义用户名
PASSWORD = "admin123" #定义密码
for i in range(3): #执行3次
input_name = raw_input("input your name:") #输入用户名
input_pass = raw_input("input your passwd:") #输入密码
if input_name == USER_name and input_pass == PASSWORD: #条件语句如果用户名密码正确中中执行以下操作不表示登录成功。
print("you are right.")
print("login is right.")
break #登录成功跳出循环
else:
print("your name is wrong.") #登录用户名密码错误输出错误。
else:
print("input too many count.") #循环完成三次登录都不成功输出。
执行结果:
执行三次错误用户名和密码:
input your name:admin
input your passwd:admin
your name is wrong.
input your name:admin
input your passwd:admin
your name is wrong.
input your name:admin
input your passwd:passwd
your name is wrong.
input too many count.
执行正确用户名密码:
input your name:admin
input your passwd:admin
your name is wrong.
input your name:admin1
input your passwd:admin123
you are right.
login is right.
在pycharm下编写程序及操作使用input输入:
USER_name = "admin1" #定义用户名
PASSWORD = "admin123" #定义密码
for i in range(3): #执行3次
input_name = input("input your name:") #输入用户名
input_pass = input("input your passwd:") #输入密码
if input_name == USER_name and input_pass == PASSWORD: #条件语句如果用户名密码正确中中执行以下操作不表示登录成功。
print("you are right.")
print("login is right.")
break #登录成功跳出循环
else:
print("your name is wrong.") #登录用户名密码错误输出错误。
else:
print("input too many count.") #循环完成三次登录都不成功输出。
测试报错:
input your name:admin
Traceback (most recent call last):
File "C:/Users/daisy/PycharmProjects/s12/day1/login��.py", line 6, in <module>
input_name = input("input your name:")
File "<string>", line 1, in <module>
NameError: name 'admin' is not defined
Process finished with exit code 1
原因input与raw_input区别:
>>> input_name = input("input your name:")
input your name:23
>>> type(input_name)
input your name:23
>>> type(input_name)
<type 'str'> #表示字符串
由此可见:input默认输出为整型,raw_input默认输出为字符串:
使用input解决方法:(将输入的值用引号引起来表示是字符串 )
input your name:"admin"
input your passwd:"admin123"
your name is wrong.
input your name:"admin1"
input your passwd:"admin123"
you are right.
login is right.
总结:由此可见2.X版本最好使用raw_input输入字符串,那么raw_input如何输入数字类型呢?
以下是方法:
>>> input_name = int(raw_input("input your name:")) #采用int表示整型:
input your name:23
>>> type(input_name)
<type 'int'> #确实是int类型:
>>> input_name = float(raw_input("input your name:")) #采用浮点:
input your name:23
>>> type(input_name)
<type 'float'> #确实是浮点类型:
>>>
四、程序以在Linux下程序:
[root@localhost ~]# python3 --version
Python 3.5.2 #版本:
[root@localhost ~]# python3
Python 3.5.2 (default, Apr 9 2017, 00:21:30)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> USER_name = "admin1"
PASSWORD = "admin123"
# input_num = 1
>>> >>> ... # input_count = 0
... for i in range(1, 4):
... input_name = input("input your name:")
... input_pass = input("input your passwd:")
... if input_name == USER_name and input_pass == PASSWORD:
... print("you are right.")
... print("login is right.")
... break
... else:
... print("your name is wrong.")
... else:
... print("input too many count.")
...
input your name:admin
input your passwd:admin
your name is wrong.
input your name:admin1
input your passwd:admin123
you are right.
login is right.
>>>
由于3.x版本已经精简所以使用input不会报错也不需要将输入的值用引号引起来表示字符。
那么我们测试以下input输入的类型:
先输入数字:
>>> input_name = input("input your name:")
input your name:23
>>> type(input_name)
<class 'str'> #也是字符串
同样的想在3.X中输入数字类型可以采用int、float等。可参考上文。