Python学习笔记四:for循环

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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循环的主要内容,如果未能解决你的问题,请参考以下文章