python学习随笔(三)

时间:2023-03-09 14:48:37
python学习随笔(三)

在linux中输入密码,我们是看不到的,如果在python中直接输入是可以看的到的,执行以下程序

#!/usr/bin/env python

username = raw_input("username:")
     password = raw_input("password:")
     print(username,password)

python学习随笔(三)

假如为了不让看到密码,该怎么样呢?python标准库里有个模块叫getpass,引入这个模块,执行以下程序。

#!/usr/bin/env python

import getpass

      username = raw_input("username:")

      password = getpass.getpass("password:")
      print(username,password)

python学习随笔(三)

可以看到执行过程输入123是看不到的了。

下面来看看if else判断

  username = input("username:")
password = input("password:") _username = 'wnl'
_password = '123'
if _username == username and _password == password:
print("Welcome {name} to login" .format(name=username))
else:
print("name or password is not right")

一定要注意格式,注意if和else语句结束后有冒号,==两个等于号是等于的意思,=是赋值的意思,不等于是!=。if下一行print一定要强制缩进,同级结构对齐,所以else不缩进,下面在执行一个小程序。

  age_of_wnl = '23'
guess_age = input("guess age:") if guess_age == age_of_wnl :
print("right")
elif guess_age > age_of_wnl:
print("smaller")
else:
print("bigger")
这个程序多了elif,多了个判断,其实还是if-else判断。
今天就分享到这里,谢谢!