发布网友 发布时间:2022-04-22 21:55
共1个回答
热心网友 时间:2022-05-10 23:46
#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author:Storm_ck
@file :20200605-01.py
@time :2020/6/5 15:20
"""
"""
猜数字,看谁猜的最接近
"""
import random
def get_abs(rannum, ansnum):
return abs(ansnum - rannum)
if __name__ == "__main__":
num = random.randint(1, 100)
adic = {}
lens = 0
while True:
choice = input("What's your name?,enter to quit:")
if choice == "enter":
break
if choice != "enter":
answer = int(input("What's your guess(1-100):"))
lens += 1
if choice in adic.keys():
adic[choice] = answer
else:
adic.setdefault(choice, answer)
newlist = sorted(adic.items(), key = lambda kv: get_abs(num, kv[1]), reverse = False)
if newlist[0][1] != newlist[1][1]:
if num == newlist[0][1]:
print("{} 厉害,数字就是{}:".format(newlist[0][0], newlist[0][1]))
else:
print("数字是{},猜的最接近的是:{}".format(num, newlist[0][0]))
else:
temp = []
alist = list(zip(*newlist))[1]
t = alist[0]
for i in range(alist.count(t)):
temp.append(newlist[i][0])
astr = ",".join(temp)
if num == t:
print("{}都比较厉害,数字就是{}:".format(astr, num))
else:
print("数字是{},{}的答案相同,猜的最接近。".format(num, astr))