设定:射门和补救只有3个方向选择“左、中、右”,补救方向电脑随机选择。
from random import choice
print("choice one side to shoot:\nleft center right")
you = input()
print("you kicked:" + you)
com = choice(["left", "center", "right"])
print("computer saved:" + com)
if you != com:
print("goal!")
else:
print("oops...")
choice one side to shoot:
left center right
center
you kicked:center
computer saved:right
goal!
random模块用于生成随机数
1、random.random():随机生成一个0到1的浮点小数
2、random.uniform(a, b):随机生成一个a到b之间的浮点小数
3、random.randint(a, b):随机生成一个a到b之间的整数
4、random.randrange(a, b , c):从 制定的范围a到b内 按照制定的基数递增 的集合 中获取一个随机数
5、random.choice(sequence):从序列sequence中获取一个随机元素,sequence可以是list、tuple、字符串
6、random.shuffle(x[, random]):将一个列表中的元素打乱
7、random.sample(sequence, k):从指定序列中随机获取指定长度的片段
random - Random variable generators.
FUNCTIONS
random(self)
Get the next random number in the range [0.0, 1.0).
uniform(self, a, b)
Get a random number in the range [a, b) or [a, b] depending on rounding.
randint(self, a, b)
Return random integer in range [a, b], including both end points.
randrange(self, start, stop=None, step=1,_int=<class 'int'>)
Choose a random item from range(start, stop[, step]).
choice(self,seq)
Choose a random element from a non-empty sequence.
shuffle(self,x, random=None)
Shuffle list x in place, and return None.
sample(self, population, k)
Chooses k unique random elements from a population sequence or set.