python学习DAY1(2)(if else while等语句)

输入数据2
在WINDOWS中不显示输入数据(和Linux中输入密码同样道理)
import getpass        #import 在python中和CC++中的include作用相同

  变量名 = getpass.getpass("xxx:")

1.简单的if-else语句
test='a'
_name = 'lbc'
_password = '990129'
username = input("username:")
password = input("password:")
if (_name == username and _password == password) :                     //注意冒号
    print("welcome {username} loging ..." .format(username=_name))
else :
    print("Invalid username or password!")
2.while语句(while true :)   

while if elif else 循环
age = '56' 
count = 0
while  count<3 :
 guess_age = input("guess_age:")
 if age == guess_age :
    print("you got it!")
    break;
 elif age <guess_age :
    print("think smaller!")
 else :
    print("think bigger!")
    
else :   //这个else 是链接前面的while语句,缩进格式对应。如果输错超过三次,则提示尝试过多次数,注意格式,等级越高,其语句越靠前(注意缩进)
    print("you tried too much!")


1.if elif else 语句加:
2.while 可以与else 连用,但需注意缩进格式,如果从while 中break跳出,则不会执行else语句
3.for 用法
for i in range(10) 10表示从0开始算起的位数
for i in range(0,10,2) 0表示从0开始,10表示总个数,如果输出i的话,2表示的是每隔一个输出
age='56'
for i in range(3) :
 guess_age = input("guess_age:")
 if age == guess_age :
    print("you got it!")
    break
 elif age <guess_age :
    print("think smaller!")
 else :
    print("think bigger!")
else :
    print("you tried too much")
4.break与continue
continue
跳出本次循环接下一个循环 break是结束循环

注意!在phython中,注意缩进问题。if条件后的语句如果有多个,要把每一行首字母对齐,并放在if下一行后面,即不与该if首字符对齐

如:
    if xxx :
       print("This is a test")
而 if xxx :
    print("This is a test") 是错误的

如
if xxx :
   print("this is a test1")
     if xxx :
        print("this is a test2")
   print("this is a test3")

假设为作用域。可以理解为test3的作用域和test1的是一致的,不管第二个If
的语句成不成立,如果第一个if语句条件成立的话,test3同样执行。

简单总结:等级越高位置越前。
原文地址:https://www.cnblogs.com/god-for-speed/p/10833667.html