python中的相关判断语句
while判断以及if判断
Fibonacci series: 斐波纳契数列
两个元素的总和确定了下一个数
a, b = 0, 1 while b < 10: print(b,end=’,’) # end可以将结果输出为一行,或者在输出的末尾添加不用的符号 a, b = b, a+b # 从左向右执行 n=b,m=a+b,a=n.b=m
# if语句
age = int(input(“请输入你家狗狗的年龄: “)) print(””) if age <= 0: print(“你是在逗我吧!”) elif age == 1: print(“相当于 14 岁的人。”) elif age == 2: print(“相当于 22 岁的人。”) elif age > 2: human = 22 + (age -2)*5 print("对应人类年龄: ", human)
该实例演示了数字猜谜游戏
numbers = 20 guess = -1 print(“猜字谜游戏”) while guess != numbers: guess = int(input(“请输入你猜到的数字:”))
if guess == numbers: print(“你猜对了!”) elif guess < numbers: print(“你猜的数字小了”) elif guess > numbers: print(“你猜的数字大了”)
计算1到100之和
n = 100 sum = 0 counter = 1 while counter <= n: sum = sum + counter counter +=1
print(“1到%d之和为:%d”%(n,sum))
while cont < 5: cont = int(input(“请输入你的数字:”)) print(cont, “数字小于5”)
else: print(cont, “数字为5了”)
for循环
sites = [“Baidu”, “Google”,“Runoob”,“Taobao”] for site in sites: if site == “Runoob”: print(“菜鸟教程!”) break print("循环数据 " + site) else: print(“没有循环数据!”) print(“完成循环!”)
|