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语句的循环范围,第三个代表步长,每几个数字循环打印一次
以上仅为自己的学习进行记录,