Python猜字游戏,有点点小技巧,哈哈

# -*- coding:utf-8 -*-

import random


def Caiziyouxi():
    secret = random.randint(1, 20)
    guess = 0  # 游戏玩家输入的数字
    tries = 0
    listHis1 = []  # 太小
    listHis2 = []  # 太大
    print("猜字游戏开始!")
    while True:
        if tries == 0:
            guess = int(input("请输入一个1到20之间的整数!\n:"))
        tries = tries + 1
        if len(listHis1) ==0 or len(listHis2) ==0:
            if guess < secret:
                listHis1.append(guess)
                print("listHis1=", listHis1, "listHis2=", listHis2)
                print("你输入的%d太小了,请重新输入!" % (guess))
                guess = int(input("请输入一个%d到20之间的整数!\n:"%(guess)))
            elif guess > secret:
                listHis2.append(guess)
                print("listHis1=", listHis1, "listHis2=", listHis2)
                print("你输入的%d太大了,请重新输入!" % (guess))
                guess = int(input("请输入一个1到%d之间的整数!\n:" % (guess)))
            elif guess == secret:
                print("恭喜你猜对了!真确答案是", guess)
                break
        else:
            if guess < secret:
                listHis1.append(guess)
                print("listHis1=", listHis1, "listHis2=", listHis2)
                print("你输入的%d太小了,请重新输入!" % (guess))
                guess = int(input("请输入一个"+str(listHis1[-1])+"到"+str(listHis2[-1])+"之间的整数!\n:"))
            elif guess > secret:
                listHis2.append(guess)
                print("listHis1=", listHis1, "listHis2=", listHis2)
                print("你输入的%d太大了,请重新输入!" % (guess))
                guess = int(input("请输入一个" + str(listHis1[-1]) + "到" + str(listHis2[-1]) + "之间的整数!\n:"))
            elif guess == secret:
                print("恭喜你猜对了!真确答案是", guess)
                break


if __name__ == '__main__':
    Caiziyouxi()

Python猜字游戏,有点点小技巧,哈哈_第1张图片

你可能感兴趣的:(python)