f=open('gameUp.txt')
lines=f.readlines() #用readlines把每组成绩分开来:
f.close()
scores={} #初始化一个空字典
for I in lines:
s=I.split() #把每一行的数据拆分成list
scores[s[0]]=s[1:] #第一项作为key,剩下的作为value,source[key]=[value]
#字典类的get方法是按照给定key寻找对应项,如果不存在这样的key,就返回空值None。
score=scores.get(name) #查找当前玩家的数据 变量name中存储的是字典的键
if score is None: #如果没找到
score=[0, 0, 0] #初始化数据
from random import randint #引入模块方法:from 模块名 import 方法名
times=0
num=randint(0,10)
print('Guess what I think?(0~10)')
begin=1
while begin==1:
times+=1 #tiimes来记录每次游戏所用的轮数
answer=int(input())
if answer>num:
print("It's too big")
elif answer<num:
print("It's too small")
elif answer==num:
print('BINGO! '+str(num)+' is the right answer!')
begin=0
#如果是第一次玩,或者本次的轮数比最小轮数还少,就记录本次成绩为最小轮数
if game_times==0 or times<min_times:
min_times=times
total_times+=times
game_times+=1
if game_times > 0: #.因为0是不能作为除数的,所以这里还需要加上判断
avg_times = float(total_times) / game_times
#对于每一项成绩,我们要将其格式化:
result='' #初始化一个空字符串,用来存储数据
for n in scores:
line=n+''+''.join(scores[n])+'\n' #把成绩按照“name game_times min_times total_times”格式化
#结尾要加上\n换行
result +=line #添加到result中
from random import randint #引入模块方法:from 模块名 import 方法名
times=0
num=randint(0,10)
print('Guess what I think?(0~10)')
begin=1
while begin==1:
times+=1 #tiimes来记录每次游戏所用的轮数
answer=int(input())
if answer>num:
print("It's too big")
elif answer<num:
print("It's too small")
elif answer==num:
print('BINGO! '+str(num)+' is the right answer!')
begin=0
#如果是第一次玩,或者本次的轮数比最小轮数还少,就记录本次成绩为最小轮数
if game_times==0 or times<min_times:
min_times=times
total_times+=times
game_times+=1
if game_times > 0: #.因为0是不能作为除数的,所以这里还需要加上判断
avg_times = float(total_times) / game_times
#对于每一项成绩,我们要将其格式化:
result='' #初始化一个空字符串,用来存储数据
for name in scores:
line=name+' '+' '.join(scores[name])+'\n' #把成绩按照“name game_times min_times total_times”格式化
#结尾要加上\n换行
result +=line #添加到result中
还真是可以实现,请问你是改了哪了呀?没看出来啊
还有个小问题如下:
Please input your name:
xb
Guess what I think?(0~10)
5
It's too small
8
It's too small
9
BINGO! 9 is the right answer!
xb,你已经玩了1次,最少3轮猜出答案,平均3.00轮猜出答案
>>>
==== RESTART: D:\Learning_Software\Python\Documents\CrossinLesson\test.py ====
Please input your name:
XB
Guess what I think?(0~10)
4
It's too small
8
BINGO! 8 is the right answer!
XB,你已经玩了1次,最少2轮猜出答案,平均2.00轮猜出答案
>>>
==== RESTART: D:\Learning_Software\Python\Documents\CrossinLesson\test.py ====
Please input your name:
xb
Guess what I think?(0~10)
8
It's too big
5
BINGO! 5 is the right answer!
XB,你已经玩了2次,最少2轮猜出答案,平均2.50轮猜出答案