if语句
if condition:
success thing
...
age = 18
if age >= 18:
print('Welcome to my home')
print('-----------------')
重命名快捷键:Shift+F6
if condition:
success thing
else:
failed thing
逻辑运算符
and
condition1 and condition2
or
not
一键注释:
ctrl+/
再重复按下:取消注释
判断用户输入的值是否为空:
简化:
if后面判断bool值
if c1:
thing
elif c2:
thing
...
else:
thing
嵌套使用:
if c1:
thing
if c2:
...
else:
...
else:
thing
练习:
在python中,要使用随机数,首先需要导入随即数模块 – ‘工具包’
导入模块后,可以直接在模块名称后面敲一个.然后Tab键,会提示该模块中>包含的所有函数
random.randint(a,b),返回[a b]之间的整数,包含a和b
eg: random.randint(12,20):生成随机数n: 12 <= n <= 20
random.randint(20,20): 结果永远是20
random.randint(20,12):结果会报错:下限必须小于上限
1.从控制台输入要出的拳 —石头(1)/剪刀(2)/布(3)
2.电脑随即出拳–先假定电脑只会出石头,完成整体代码功能
3.比较胜负
石头 胜 剪刀
剪刀 胜 布
布 胜 石头
导入随即工具包
注意,在导入工具包的时候,应该将导入的语句,放在文件的顶部
因为,这样可以方便下方的代码,在任何需要的时候,使用工具包中的工>具
import random
#1、player
player = int(input('Your choice:'))
#2、computer
computer = random.randint(1,3)
print(computer)
#3、compare
if (player == 1 and computer == 2) or (player == 2 and computer == 3) or (player == 3 and computer == 1):
print('Player win!')
elif player == computer:
print('No winner!')
else:
print('Computer win!')