竞猜游戏小玩
1 # -*- coding:utf-8 -*- 2 3 import random 4 5 go =1 6 while go == 1: 7 num_start = int(raw_input('请输入开始的数字:')) 8 num_end = int(raw_input('请输入结束的数字:')) 9 f = int(raw_input('请输入循环的次数:')) 10 pk = int(raw_input('pk的次数:')) 11 userdict = {} 12 13 def host(num_start,num_end): 14 rand = random.randint(num_start,num_end) 15 return rand 16 17 for time in range(pk): 18 a = 0 19 rand = host(num_start, num_end) 20 print "开始第", time + 1, "轮" 21 for i in range(f): 22 user = raw_input("请输入名字:") 23 if not (user in userdict): 24 userdict[user] = 0 25 guess = int(raw_input("请输入数字:")) 26 if guess == rand: 27 print "您赢了!" 28 a = 1 29 if user in userdict: 30 userdict[user] += 1 31 break 32 elif guess > rand: 33 print ("大于随机数。") 34 else: 35 print "小于随机数。" 36 print "这轮你还有",(f-i-1),"次机会。" 37 if a != 0: 38 break 39 if a == 0: 40 print ("你的数次用完了。") 41 print "正确答案为", rand 42 go = int(raw_input("继续请输入1,结束请输入0。")) 43 44 def rank(dict): 45 dict2 = sorted(dict.items(), key=lambda asd: asd[1], reverse=True) # 给字典排序,reverse=True 表示升序 46 # print dict2 47 print("----------排行榜------------") 48 for key in dict2: 49 print(key[0] + "猜对了" + str(key[1]) + "次") 50 print("----------------------------") 51 52 if __name__ == "__main__": 53 rank(userdict)