python学习---1

1,用户输入:
明文显示密码

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

密文形式:
需要加入一个python自带的getpass模块

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

2,if else判断语句

import getpass
_username = "abc"
_password = "123"
username = input("username:")
password=getpass.getpass("password:")
if _username==username and _password==password:	
     print("welcome user {name}:".format(name=username))
else:
      print("Invalid username")

其中format增强了字符串格式化的功能。

age_of_boy=34
age = input("age:")
 if age == age_of_boy :
     print("you got it")
elif age > age_of_boy:
     print("you think big")
else:
      print("you think small")

此处需要注意的是,直接使用
age = input(“age:”)会出现错误提示:

: '>' not supported between instances of 'str' and 'int'

这是因为input默认输入为字符串形式,这里需要进行转换

age =int( input("age:"))

3,while 语句

age_of_boy=34
count = 0
while count < 4:
#for i in range(3):
    age = int(input("age:"))
    **if** age == age_of_boy :
        print("you got it")
        break    #break表示结束本次循环,而continue是跳出此次循环,进入下一循环
    **elif** age > age_of_boy:
        print("you think big")
    **else**:
        print("you think small")
    count=count+1  #或者count+=1
    if count==4:
        countine_confirm=input("do you want to continnue?")
        if countine_confirm!="n":
            count =0
else:
    print("stop")

扩展

for i in range(0,10,2)
     print("output:", i)

range()函数中,前两个数字代表的本次for语句的循环范围,第三个代表步长,每几个数字循环打印一次
以上仅为自己的学习进行记录,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值