import random,requests
def ran_num():
url = 'https://python666.cn/cls/number/guess/'
r = requests.get(url)
return r.text
def num_input(): # 强制用户输入数字
while True:
str = input("请猜一个1--100的整数字:")
if not str.isdigit():
print(f"你输入的{str}不符合游戏规则,请重新输入:")
elif int(str) not in range(1, 101):
print(f"你输入的{str}不在数值范围内,请重新输入:")
else:
return int(str)
def guess_game(name,cishu,lunshu,ave_lun): #猜数据的主体程序,# 次数,轮数,平均轮次,每次猜中轮数
list1=[]#每次猜中轮数的集合
while True:
answer = int(ran_num()) #带入从网上获取的随机数
times = 0
cishu += 1
while True:
times += 1
lunshu += 1
b = num_input() #带入强制用户输入的数字
ave_lun = lunshu / cishu
if answer > b:
print("小了,小了,再来")
elif answer < b:
print("大了,大了,再来")
else:
list1.append(times)
kuai_lun = min(list1)
print("ok,你一共猜了{}轮。".format(lunshu))
print("{},你已经玩了{}次,最少{}猜出,平均{}猜出答案".format(name, cishu, kuai_lun, ave_lun))
break
if input("y继续,其他退出") != ('Y' and 'y'):
print("退出游戏,下次再来")
break
with open("game_many_user.txt" )as f:
lst_a = f.readlines()
dic_a = {}
list2 = []
for i in lst_a:
data = i.strip().split(" ")
list2.append(data[0])
# print(list2) # 所有玩家名字的列表
# print(data) #这个是各个玩家数据的列表形式
# print(data[0], data[1:])
dic_a[data[0]] = data[1:]
print('dic_a:', dic_a) # 玩家的数据形式将以字典结构呈现
def main():
name = input("尊姓大名:")
if name in list2:
print("你已经玩了{}次,最少{}猜出答案,平均{}轮猜出.开始游戏吧".format(dic_a[name][0], dic_a[name][1], dic_a[name][2]))
cishu = int(dic_a[name][0])
lunshu = int(dic_a[name][1])
ave_lun = float(dic_a[name][2])
guess_game(name,cishu,lunshu,ave_lun)
else:
print(f"{name},已经玩了0次,最少0轮猜出答案,平均0轮猜出,开始游戏吧")
cishu = 0
lunshu = 0
ave_lun = 0
lst = []
guess_game(name,cishu,lunshu,ave_lun)
dic_a[name] = [str(cishu), str(lunshu), str(ave_lun)]
with open("game_many_user.txt", 'r+', encoding='utf-8') as f2:
for key in dic_a:
data_line = key + ' ' + ' '.join(dic_a[key]) + '\n'
f2.write(data_line)
main()