首页 > 代码库 > Python学习笔记四:for循环

Python学习笔记四:for循环

一个非常简单的示例:

1 for i in range(0, 10, 2):    #0到10为循环范围,2为循环步长
2     print("Count: ", i)

使用for循环优化前面的例子:

age_konishi = 23

for i in range(3):    #默认步长为1
    guess_age = int(input("Age: "))
    if guess_age == age_konishi:
        print("Yoshi! Sugoi!")
        break
    elif guess_age > age_konishi:
        print("You‘re so bad!")
    else:
        print("Honey, you‘re so sweetie!")
else:
    print("Idiot! I give you too many times!")

 

Python学习笔记四:for循环