if True: print("hello") print("world!")
输出:dom
hello
world!函数
""" 问题:输入一个年龄。若大于18岁,则能够上网 重点:因为input输入的是字符串类型,故不能与18比较,因此要进行int类型转换 """ age = int(input("请输入您的年龄:")) if age > 18: print(f'你的年纪是{age},能够上网!')
输出:spa
请输入您的年龄:19
你的年纪是19,能够上网!3d
age = int(input("请输入您的年龄:")) if age < 18: print(f'你的年纪是{age},过小了!') elif(age >= 18) and (age <= 60): print(f'您的年纪是{age},合法年纪!') elif age > 60: print(f'您的年纪是{age},太老了!')
注释: (age >= 18) and (age <= 60) 能够简化为:18 <= age <= 60code
""" money = 1,表明有钱,反之没钱 seat = 1,表明有座,反之没座 """ money = 1 seat = 0 if money == 1: print("有钱,请上车") if seat != 1: print(" 没座,请站着") else: print(" 有座,赶忙坐") else: print("没钱,后边跑")
案例:猜拳游戏blog
扩充:随机数游戏
一、导入random模块字符串
import random二、使用函数input
random.randint(开始,结束)
""" 1、出拳 玩家:手动输入:plater 剪刀:0、石头:1,布:2 电脑:随机输入 随机数:computer 2、判断 玩家赢 玩家剪刀,电脑布 玩家石头,电脑剪刀 玩家布,电脑石头 平局 玩家剪刀,电脑剪刀 玩家石头,电脑石头 玩家布,电脑布 电脑赢 玩家剪刀,电脑石头 玩家石头,点胶布 玩家布,电脑剪刀 """ import random computer = random.randint(0,2) player = int(input("请出拳:0--剪刀,1--石头,2--布:")) print(f'电脑:{computer}') if((player == 0) and (computer ==2 ) or (player == 1) and (computer == 0) or (player == 2) and (computer == 1)): print("玩家获胜!") elif player == computer: print("平局") else: print("电脑获胜")
语法:条件成立执行的表达式 if 条件 else 条件不成立执行的表达式 for循环
a = 11 b = 22 c = a + b if a < b else a - b print(c)
语法:
举例1:1-100整数相加,而后输出结果
""" 1-100整数相加,而后输出结果 """ i = 1 result = 0 while i <= 100: result += i i += 1 print(f'1+....+100={result}')
举例2:1-100内的偶数相加,而后输出结果
方法1:
i = 0 even_result = 0 while i <= 100: even_result += i i += 2 print(f'0+2+...+100={even_result}')
方法2:
i = 1 even_result = 0 while i <= 100: if i % 2 == 0: even_result += i i += 1 print(f'0+2+...+100={even_result}')
""" j=0 j=1 j=2 j=3 i的第1遍循环 j=0 j=1 j=2 j=3 i的第2遍循环 j=0 j=1 j=2 j=3 i的第3遍循环 j=0 j=1 j=2 j=3 i的第4遍循环 i的循环结束 """ i = 0 while i <= 3: j = 0 while j <= 3: print(f'j={j}') j += 1 print(f'i的第{i+1}遍循环') i += 1 print("i的循环结束")
举例3:
""" 打印星号:正方形 ***** ***** ***** ***** ***** """ i = 0 while i <= 4: j = 0 while j <= 4: print("*",end="") j += 1 print() i += 1
举例4:
""" 打印星号:三角形 * ** *** **** ***** """ i = 0 while i <= 4: j = 0 while j <= i: print("*",end="") j += 1 print() i += 1
举例5:九九乘法表
""" 九九乘法表 1 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 """ i = 1 while i <= 9: j = 1 while j <= i: print(f'{i} * {j} = {i*j}',end="\t") j += 1 print() i += 1
break
终止整个循环
""" i == 4时,终止整个循环 """ i = 1 while i <= 5: if i == 4: print("结束了") break print(i) i += 1输出:
1
2
3
结束了continue
退出当前一次循环而执行下一次循环
""" i == 4时,终止整个循环 在使用continue以前必定要修改计数器,不然陷入死循环 """ i = 1 while i <= 5: if i == 4: print("跳过") i += 1 continue print(i) i += 1输出:
1
2
3
跳过
5
循环正常执行完毕后,才执行else的内容:
""" 5 4 3 2 1 结束了 """ i = 5 while i > 0: print(i) i -= 1 # 循环正常结束后才执行 else: print("结束了")
break的使用:
""" 5 4 3 """ i = 5 while i > 0: print(i) i -= 1 if i == 2: break # 循环正常结束后才执行 else: print("结束了")
continue的使用:
""" 5 4 3 跳过2 1 结束了 """ i = 5 while i > 0: if i == 2: i -= 1 print("跳过2") continue print(i) i -= 1 # 循环正常结束后才执行 else: print("结束了")
语法:
break:
""" for循环中使用break: h e l l o w o 遇到r结束 """ src = 'helloworld' for i in src: if i == 'r': print("遇到r结束") break print(i)continue:
""" for循环中使用continue: h e l l o w o 遇到r跳过 l d """ src = 'helloworld' for i in src: if i == 'r': print("遇到r跳过") continue print(i)
""" h e l l o w o r l d 结束循环 """ src = 'helloworld' for i in src: print(i) # for 循环正常结束后,才会执行else else: print("结束循环")
break的使用:
""" h e l l o w o r """ src = 'helloworld' for i in src: print(i) if i == 'r': break # for 循环正常结束后,才会执行else else: print("结束循环")
continue的使用:
""" h e l l o w o 跳过r l d 结束循环 """ src = 'helloworld' for i in src: if i == 'r': print("跳过r") continue print(i) # for 循环正常结束后,才会执行else else: print("结束循环")