1. random的一些函数
random.random()函数是这个模块中最常用的方法了,它会生成一个随机的浮点数,范围是在0.0~1.0之间。
random.uniform()正好弥补了上面函数的不足,它可以设定浮点数的范围,一个是上限,一个是下限。
random.randint()随机生一个整数int类型,可以指定这个整数的范围,同样有上限和下限值,python random.randint。
random.choice()可以从任何序列,比如list列表中,选取一个随机的元素返回,可以用于字符串、列表、元组等。
random.shuffle()如果你想将一个序列中的元素,随机打乱的话可以用这个函数方法。
random.sample()可以从指定的序列中,随机的截取指定长度的片断,不作原地修改。
方法一:这个方法用random.sample()
import random
#玩家出拳
punches=['剪刀','石头','布']
player_choice=input('请输入剪刀,石头,或布:')
#游戏规则
def game_rules(computer_choice,player_choice):
if computer_choice == player_choice:
print('平局')
elif ((computer_choice == '剪刀' and player_choice=='石头') or (computer_choice == '石头' and player_choice=='布') or (computer_choice == '布' and player_choice=='剪刀')):
print('你赢了')
else:
print('你输了')
def main():
computer=random.sample(punches,1) #电脑随机出拳,注意这里random.sample生成的结果是列表,需要提出里面元素,否则不能字符比较
computer_choice=computer[0] #提取列表里的元素,为下面字符比较
print('————战斗过程————')
print('电脑出了:%s ' % computer_choice) #注意里面的%格式输出,中间没有逗号,否则错误
print('玩家出了:%s ' % player_choice)
print('')
print('————战斗结果————')
game_rules(computer_choice,player_choice)
main()
>>>
请输入剪刀,石头,或布:石头
————战斗过程————
电脑出了:石头
玩家出了:石头
————战斗结果————
平局
方法二:使用random.choice(),更加简单
import random
punches=['布','石头','剪刀']
player_choice=input('请输入剪刀,石头,或布:')
def game_rules(computer_choice,player_choice):
if computer_choice == player_choice:
print('平局')
elif ((computer_choice == '剪刀' and player_choice=='石头') or (computer_choice == '石头' and player_choice=='布') or (computer_choice == '布' and player_choice=='剪刀')):
print('你赢了')
else:
print('你输了')
def main():
computer_choice=random.choice(punches)
print('————战斗过程————')
print('电脑出了:%s ' % computer_choice)
print('玩家出了:%s ' % player_choice)
print('')
print('————战斗结果————')
game_rules(computer_choice,player_choice)
main()
3.index()函数
python内置index()函数
index() :检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。如果包含子字符串返回开始的索引值,否则抛出异常。
语法:str.index(str, beg=0, end=len(string))
- str – 指定检索的字符串
- beg – 开始索引,默认为0。
- end – 结束索引,默认为字符串的长度。
abc = ["M", "K", "O", "L"]
a = abc.index("K")
print(a)
>>>
1
升级版程序
import random
punches=['布','石头','剪刀']
player_choice=input('请输入剪刀,石头,或布:')
def game_rules(computer_choice,player_choice):
if computer_choice == player_choice:
print('平局')
# 电脑的选择有3种,索引位置分别是:0石头、1剪刀、2布,或者 -3石头、-2剪刀、-1布表示。
# 电脑索引位置上减1,对应:-1布,0石头,1剪刀,玩家就都能获胜。
elif player_choice == punches[punches.index(computer_choice)-1]:
print('你赢了')
else:
print('你输了')
def main():
computer_choice = random.choice(punches)
print('————战斗过程————')
print('电脑出了:%s ' % computer_choice)
print('玩家出了:%s ' % player_choice)
print('')
print('————战斗结果————')
game_rules(computer_choice,player_choice)
main()
注意区别
import random
punches=['布','石头','剪刀']
player_choice=input('请输入剪刀,石头,或布:')
def game_rules(computer_choice,player_choice):
if computer_choice == player_choice:
print('平局')
elif computer_choice == punches[punches.index(player_choice)-1]:
print('你赢了')
else:
print('你输了')
def main():
computer_choice=random.choice(punches)
print('————战斗过程————')
print('电脑出了:%s ' % computer_choice)
print('玩家出了:%s ' % player_choice)
print('')
print('————战斗结果————')
game_rules(computer_choice,player_choice)
main()