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 结束整个循环