编程语言
首页 > 编程语言> > 用python写猜数字游戏

用python写猜数字游戏

作者:互联网

猜数字游戏

从名单中随机抽取5位同学,参加猜数字

目的:练习文件操作、循环、判断、构造函数、列表和集合的操作

主要思路:

张三
李四
王五
狗四
李好
张四
王麻子
胡八一
李敏
朱三
周二
曹三
曹达
小鬼子

版本一:

"""
从名单中随机抽取5位同学,参加猜数字
"""
import random
def main():
    # 2、随机抽取5个人员
    peopleNum = 5  #抽取5人
    people(peopleNum)
    # 3、随机生成数字1~100的数
    number = random.randint(1,100)
    # 4、判断正确
    referee(number)
#2、随机抽取5个人员
def people(peoplNum):
    peopleList = set()
    line = fileLine()

    while  len(peopleList) < peoplNum :   #避免了猜数字人员名字的重复问题
        # 生成随机数
        randNum = random.randint(1, line)
        with open("name", 'r', encoding="utf-8") as  f:   #可以不用close()文件
            for i in range(1, randNum + 1):    #目的:一次遍历文件的内容,找到指定的行(randNum就是行号)
                name = f.readline().strip('\n')  #去掉名字中的\n
                if randNum == i:
                    peopleList.add(name)   #将名字添加到人员名字中peopleList
    print("恭喜",peopleList,"进行猜数比赛")

#2.1 、获取名单的行数
def fileLine():
    with open("name","r",encoding="utf-8") as f:
        linecount = 0
        for line in f:
            linecount +=1
    return linecount

#4、判断正确
def referee(number):
    max = 100
    min = 1
    print(number)
    while True:
        inputNum = int(input("请输入%d~%d整数:\n"%(min,max)))
        if inputNum == number:
            print("恭喜你猜中了",number)
            break
        elif inputNum >= min and inputNum < number:
            min = inputNum
        elif inputNum <= max and inputNum > number:
            max = inputNum


if __name__ == '__main__':
    main()

版本二:

"""
从名单中随机抽取5位同学,参加猜数字
"""
import random
def main():
    # 2、随机抽取5个人员
    peopleNum = 5  #抽取5人
    people(peopleNum)
    # 3、随机生成数字1~100的数
    number = random.randint(1,100)
    # 4、判断正确
    referee(number)
#2、随机抽取5个人员
def people(peoplNum):
    peopleList = []  #人员的名字
    radNum = []     #保存随机数,为避免随机数产生相同
    line = fileLine()
    for x in range(peoplNum):
        # 生成随机数
        randNum = random.randint(1, line)
        radNum.append(randNum)  
        if radNum.count(randNum) > 1:   #统计数随机数产生的的个数
            continue
        with open("name", 'r', encoding="utf-8") as  f:
            for i in range(1, randNum + 1):   
                name = f.readline().strip('\n')
                if randNum == i:
                    peopleList.append(name)
    print(peopleList)
#2.1 、获取名单的行数
def fileLine():
    with open("name","r",encoding="utf-8") as f:
        linecount = 0
        for line in f:
            linecount +=1
    return linecount

#4、判断正确
def referee(number):
    max = 100
    min = 1
    print(number)
    while True:
        inputNum = int(input("请输入%d~%d整数:\n"%(min,max)))
        if inputNum == number:
            print("恭喜你猜中了",number)
            break
        elif inputNum >= min and inputNum < number:
            min = inputNum
        elif inputNum <= max and inputNum > number:
            max = inputNum


if __name__ == '__main__':
    main()

版本三:

"""
从名单中随机抽取5位同学,参加猜数字
"""
import random
import linecache
def main():
    # 2、随机抽取5个人员
    peopleNum = 5  #抽取5人
    people(peopleNum)
    # 3、随机生成数字1~100的数
    number = random.randint(1,100)
    # 4、判断正确
    referee(number)
#2、随机抽取5个人员
def people(peoplNum):
    peopleList = []  #人员的名字
    line = fileLine()
    radNum = []  # 保存随机数,为避免随机数产生相同
    while len(peopleList) < peoplNum:
        index = random.randint(1,line)   
        radNum.append(index)
        if radNum.count(index) > 1:  # 统计数随机数产生的的个数,避免名单人员名字重复
            continue
        peopleList.append(linecache.getline("name",index).strip("\n"))
    print(peopleList)
#2.1 、获取名单的行数
def fileLine():
    with open("name","r",encoding="utf-8") as f:
        linecount = 0
        for line in f:
            linecount +=1
    return linecount

#4、判断正确
def referee(number):
    max = 100
    min = 1
    while True:
        inputNum = int(input("请输入%d~%d整数:\n"%(min,max)))
        if inputNum == number:
            print("恭喜你猜中了",number)
            break
        elif inputNum >= min and inputNum < number:
            min = inputNum
        elif inputNum <= max and inputNum > number:
            max = inputNum


if __name__ == '__main__':
    main()

标签:__,peopleList,数字,游戏,min,python,number,inputNum,name
来源: https://blog.csdn.net/Andy86666/article/details/120299116