1,while
檢測(cè)輸入的值 是否合適的,while簡(jiǎn)單應(yīng)用。
_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的常規(guī)用法
_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循環(huán)的用法
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")