Python4--if-else、while循环


  1 color = raw_input ( "你白吗? \n " )
  2 money = int ( raw_input ( "你的财产 \n )) # money 转换为int类型
  3 beautiful = raw_input ( "你美么? \n " )
  4
  5 if color == "白" and money >= 100 and   beautiful == "美" :
  6     print ( "你是白富美" )
  7 else :
  8     print ( "你是矮穷矬” )

注意 : if else语句 后面的  :  切勿在中文模式下输入

比较运算符
== 
!=   ——   <>   都是不等于
>=
<=
>
<

逻辑运算符
and        a=1 and b=1
or            a=1 or b=1
not        not(a =1 and b=1)


if—elif—else
if  条件:
    xxx
elif条件:
    xxx
elif条件:
    xxx
else条件:
    xxx

例句:
  1 age = int ( input ( "输入你的年龄: \n " ))
  2 if age < 6 :
  3     print ( "你还没有上学" )
  4 elif age <= 12 :
  5     print ( "在上小学" )
  6 elif age <= 18 :
  7     print ( "在上中学" )
  8 else :
  9     print ( "你成年了" )


while循环
 10
 11 while age <= 18 :
 12     print (age)
 13     age =age + 1

print(“*”),    #python 不换行
print(“*”,end=“”)  #python3 不换行

break 用法
结束 while循环
continue 用法
跳出本次 while循环(即while中 continue后面代码跳过不执行)
一个break 或者 continue 对应一个while 生效

  1 i = 1 ;
  2 while i <= 10 :
  3     #打印偶数
  4     if i% 2 == 0 :
  5         print (i);
  6     i += 1
  7     if i == 6 :
  8         break
  9 print ( "结束" )
 10
 11 while i<= 20 :
 12     i += 1
 13     if i% 3 == 0 :
 14         print (i)
 15     if i == 15 :
 16         continue
 17     print ( "====" )

for循环
  1 nums = [ 12 , 23 , 34 , 45 , 56 , 67 , 78 ]
  2 i = 0
  3 while i< len (nums):
  4     print (nums[i])
  5     i+= 1
  6 print ( '=' * 40 )
  7 for num in nums:
  8     print (num)



猜你喜欢

转载自blog.csdn.net/csdn15150525313/article/details/78388915
今日推荐