tutu小白python-day2---if..while..for

if 判断
定义变量age=25,如果输入小于25 print you are thinks bigger
大于25 print you are thiks smaller
等于25则print you are git

name =input("name:")
age =25
tutuage =int(input("tutuage:"))
if age == tutuage:
 print ("you are git")
elif age >tutuage:
 print ("you are thinks bigger")
else:
 print("thinks smaller")

while循环 一直循环,直到对了跳出循环

age = 25
count =0
while True:
 tutuage =int(input("tutuage:"))
 if age == tutuage:
  print ('this is git')
  break
 elif age > tutuage:
  print ('you are thinks bigger')
 else:
  print ('you are thinks smaller')
 count +=1

while 定义循环次数
如果正确则跳出循环,如果输入错误5次也跳出循环

age =25
count =0
while True:
 tutuage =int(input("tutuage:"))
 if age == tutuage:
  print ('ok')
  break
 if count ==4:
  break
 count +=1

猜测年龄,做多三次,对了跳出循环,三次不对也跳出循环print you get fuck off

age =25
count =0
while count <3:
 tutuage =int(input("tutuage:"))
 if age == tutuage:
  print ('this is git')
  break
 elif age > tutuage:
  print ('you are thinks bigger')
 else:
  print ('you are thinks smaller')
 count +=1
else:
 print ('you get fuck off')

for 循环使用

for i in range(3):
	print ('qiqi',i)
结果如下:
qiqi 0
qiqi 1
qiqi 2

用for实现上边的猜测年龄,正确跳出,错误三次跳出

age =25
for i in range(3):
 tutuage =int(input("tutuage:"))
 if age == tutuage:
  print ('this is git')
  break
 elif age > tutuage:
  print ('you are thinks bigger')
 else:
  print ('YOU ARE THINKS SMALLER')
else:
 print ('you get fuck off')

如果三次都不对,按回车键继续,n键退出

age =25
count =0
while count <3:
 tutuage =int(input("tutuage:"))
 if age == tutuage:
  print ('this is git')
  break
 elif age > tutuage:
  print ('you are thinks bigger')
 else:
  print ('you are thinks smaller')
 count +=1
 if count ==3:
  countine_confirm =input ('do you want guee keep...')
  if countine_confirm != 'n':
   count =0

小结:
continue 跳出本次循环,继续下次循环
break 结束整个循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值