python if else while for

Python密码与循环实践
本文介绍了使用Python的getpass模块来安全地输入密码的方法,并通过示例演示了如何使用循环结构进行年龄猜测游戏,包括基本的for和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)

 

  

  

  

 

转载于:https://www.cnblogs.com/qing-chen/p/7152062.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值