一个非常简单的示例:
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!")
时间: 2024-10-10 13:52:05