猜数游戏-人机对战-经典的randint使用

   翻阅去年的笔记,老师曾经教的random模块下的三种用法,其中之一是randint用法,今天上传,留作笔记参考。人生苦短,我用python!

      

# -*- coding: UTF-8 -*-
import  random
# 游戏玩法 两人输入两个数,是否比电脑随机生成的数大
people1 = int (input("请输入您要选择的数:1-1000范围的数"))
people2 =  int (input("请输入您要选择的数:1-1000范围的数"))
# 做一个循环,让系统循环生成,当他是相等就结束
 # 系统自动生成的数
# 定义一个i变量,从1开始
i = random.randint(1,1000000)
j = random.randint(1,1000000)
# 赢的次数win  输的次数faile  相等的次数 same
win=0
faile=0
same=0
while i < j:
    computer1 = random.randint(1,1000000)  # 电脑1  随机生成数
    computer2 = random.randint(1,1000000)  # 电脑2  随机生成数
    totle_people = people1+people2      # 用户输入值的总和
    totle_computer = computer1 + computer2   # 系统随机生成数的总和
 # 开始作为判断
    if totle_computer > totle_people:
            print("对不起您输了,系统的值比您的大!系统生成的值和为:%d,您的值和为:%d请重新输入,谢谢!"%(totle_computer,totle_computer))
            faile+=1

    elif totle_computer== totle_people:
            print("您的值和系统的值数相等的,系统的值为:%d,您的值为:%d,厉害哦!"%(totle_computer,totle_people))
            same+=1
    else:
            print("您赢了,您战胜了电脑了!此时系统的值和为:%d,您生成的值和为:%d"%(totle_computer,totle_people))
            win+=1
    i+=1
print("统计:赢的次数为:%d,输的次数为%d,平局的次数为:%d"%(win,faile,same))

  

猜你喜欢

转载自www.cnblogs.com/68xi/p/9351344.html