1,while
检测输入的值 是否合适的,while简单应用。
_age = 30
count = 0
total = 3
while count < total:
age = int(input("guess age : "))
if _age < age:
print("it's larger")
elif _age == age:
print("you got it")
break
else:
print("it's smaller")
count += 1
if total - count > 0:
print("you already left %d times" % (total - count))
else:
print("you have already used all your chance, Bye"
or 不带else的常规用法
_age = 30
count = 0
total = 3
while count < total:
age = int(input("guess age : "))
if _age < age:
print("it's larger")
elif _age == age:
print("you got it")
break
else:
print("it's smaller")
count += 1
if total - count > 0:
print("you already left %d times" % (total - count))
2,for
for循环的用法
for i in range(0,10,2):
print(i)
_age = 2
count = 0
total = 3
for i in range(3):
age = int(input("guess age : "))
if _age < age:
print("it's larger")
elif _age == age:
print("you got it")
break
else:
print("it's smaller")
if total - i-1 > 0:
print("you already left %d times" % (total - count))
else:
print("you have already used all your chance, Bye")
网友评论