Python3基础-条件语句

if 语句

Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false。
Python 编程中 if 语句用于控制程序的执行,基本形式为:
if 语句的判断条件可以用>(大于)、<(小于)、==(等于)、>=(大于等于)、<=(小于等于)、!= (不等于)来表示其关系
if 判断条件:
       执行语句……
else:
      执行语句……

eg:

 

#if 语句
#将用户输入的用内容赋值给 username 变量
username=input('请输入用户名:')
if username == 'sugh':  #判断username的值
    print('账号正确')
else:
    print('账号错误')

 

当判断条件为多个值时,可以使用以下形式:
if 判断条件1:
       执行语句1……
elif 判断条件2:
       执行语句2……
elif 判断条件3:
      执行语句3……
else:
      执行语句4…

 

#if 语句
#将用户输入的用内容赋值给 username 变量
username=input('请输入用户名:')
passwd=input('请输入密码:')

if username == 'sugh':  #判断username的值
    print('欢迎你,高级团长')
elif username == 'sugh1':
    print('欢迎你,普通团长')
elif username == 'sugh2':
    print('欢迎你,VIP')
else:                     #条件不满足 为sugh、sugh1、sugh2时输出
    print('欢迎你',username)

#打印输入的内容
print('username==%s,passwd==%s'%(username,passwd))

 

 

 

while循环

# -*- encoding: utf-8 -*-
count=0
while count < 9:
    print('The count is ',count)
    count = count + 1
    print('good bye')

 


以上代码执行输出结果
The count is 0
The count is 1
The count is 2
The count is 3
The count is 4
The count is 5
The count is 6
The count is 7
The count is 8
good bye

无限循环

while 1==1: #该条件永远为true,循环将无限执行下去
         执行语句1……

注意:以上的无限循环你可以使用 CTRL+C 来中断循环。

continue,break

# -*- encoding: utf-8 -*-
# while循环
count=0
while count < 9:
    count = count + 1
    if count % 2 > 0: #非双数时跳过输出
        continue
    print('The count is ',count) #输出双数2、4、6、8
else:
    print("当前循环结束啦啦啦啦")


i=1
while 1:  #循环条件为1必定成立
    print("i====",i)
    i +=1
    if i>10: #当大于10时跳出循环
        break

 

while循环使用 else 语句

# -*- encoding: utf-8 -*-
count=0
while count < 9:
    count = count + 1
    if count % 2 > 0: #非双数时跳过输出
        continue
    print('The count is ',count) #输出双数2、4、6、8
else:
    print("当前循环结束啦啦啦啦")

 

posted @ 2019-10-12 10:15  槑槑DE  阅读(251)  评论(0编辑  收藏  举报