目录
if 条件语句:
相关用法:if-or;if-and;if-not;if-elif
#if-or
you = input("你去么?") # 去或者不去
yourWife = input("你老婆去么?") #去或者不去
#if you=="去" 或者 yourWife=="去":
if you=="去" or yourWife=="去":
print("可以成功。。。")
## if-and
you = input("你去么?") # 去或者不去
yourWife = input("你老婆去么?") #去或者不去
#if you=="去" 并且 yourWife=="去":
if you=="去" and yourWife=="去":
print("可以成功....")
#if-not
a = 30
if not (a>0 and a<=50):
print("在0到50之间....")
#if-elif
sex = input("请输入你的性别:")
if sex == "男":
print("你是男性....")
elif sex == "女":
print("你是女性....")
#elif sex == "中性":
else:
print("你是第3性别.....")
while使用:
while 的基本形式
while <判断条件>:
执行语句(逻辑操作)........
ps:条件判断后的,逻辑操作可以有多行。
while用法:
i = 1
while i<=100:
print("%d"%i)#print(i)
i = i+1
for的使用:
for item in sequence:
执行语句
for用法:
for i in range(1,10):#使用range设立范围(1到9)
print(i)
###
sum = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for i in sum :
print(i)
练习打印99乘法表:
i = 1
while i<=9:
j = 1
while j<=i:
print("%d*%d=%d\t"%(j,i,i*j), end="")
j+=1
print("")
i+=1
###
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
逻辑实现石头剪子布:
import random #random为随机生成包
#1. 提示并获取用户的输入
player = int(input("请输入 0剪刀 1石头 2布:"))
#2. 让电脑出一个
computer = random.randint(0,2)
#2. 判断用户的输入,然后显示对应的结果
#if 玩家获胜的条件:
if (player==0 and computer==2) or (player==1 and computer==0) or (player==2 and computer==1):
print("赢了,,,,")
#elif 玩家平局的条件:
elif player==computer:
print("平局了,,")
else:
print("输了,,,")
总结:
大家了解if、while、for相关用法即可,这是程序书写的核心,需要在实战中大量练习复杂问题。
作为视频替代者,我认为各种视频虽然经过精简,但是还是冗余啰嗦。如果需要认真学习,对应视频讲解链接:https://download.csdn.net/download/weixin_40651515/12366796
注:range()函数输出的序列包含左面数字,不包含右面数字。
random()是不能直接访问的,需要导入 random 模块,然后通过 random 静态对象调用该方法。