首页 > 代码库 > python if else while

python if else while

1 getpass模块 设置密码不显示明文

 

用户名和密码输入程序:

import getpass

username = input("username:")
password = getpass.getpass("password:")

print(username,password)

技术分享

 

判断登录

_username = "jj"
_password = ‘123456‘
username = input("username:")
password = getpass.getpass("password:")

if _username == username and _password==password:
    print(‘welcome login..‘)
else:
    print(‘Invalid username or password‘)

技术分享

 

猜年龄:

age_jj = 35
guess_age = int(input(‘guess age:‘))
if guess_age == age_jj:
    print(‘YES‘)
elif guess_age > age_jj:
    print(‘think smaller..‘)

else:
    print("think bogger..")

技术分享

猜3次 猜对了就退出,错误次数达到3次也退出

while 

while 条件   循环语句 如果提交为真就一直执行

age_jj = 35
count = 0
while count< 3 :
    guess_age = int(input(‘guess age:‘))
    if guess_age == age_jj:
        print(‘YES‘)
        break
    elif guess_age > age_jj:
        print(‘think smaller..‘)

    else:
        print("think bogger..")
    count += 1

技术分享

age_jj = 35
count = 0
while count< 3 :
    guess_age = int(input(‘guess age:‘))
    if guess_age == age_jj:
        print(‘YES‘)
        break
    elif guess_age > age_jj:
        print(‘think smaller..‘)

    else:
        print("think bogger..")
    count += 1
else:
    print(‘you have tried too many times.. fuck you..‘)

技术分享

while else"

如果while 的条件为真 就一直执行while 里面的代码

如果while的条件为假 就执行else里面的代码

for  

  Python的for循环有两种:

1 利用range()函数生成一个整数序列。

print(type(range(10)))
for i in range(10):
    print(i)

技术分享

 

这个序列可以转成列表

print(list(range(10)))

技术分享

 

2 for ..in 循环列表,依次把列表中的元素迭代出来

 

names=[‘michael‘,‘Bob‘,‘Tracy‘]
for name in names:
    print(name)

技术分享

 

  

  

  

 

python if else while