from random import randint
print '输入你的名字:'
name = raw_input()
f = open('game.txt')
lines = f.readlines()
scores = {}#初始化一个空字典
for l in lines:
s = l.split()
scores[s[0]] = s[1:]
#print s[:]-----------------['yaoxiaokui', '1', '6', '6']
score = scores.get(name)
if score is None:
score = [0, 0, 0]
game_times = int(score[0])
min_times = int(score[1])
total_times = int(score[2])
if game_times > 0:
avg_times = float(total_times) / game_times
else:
avg_times = 0
print '%s,你已经玩了%d次,最少%d轮猜出答案,平均%.2f轮猜出答案' %(name, game_times, min_times,avg_times)
print '输入数值的最大值'
max = input()
num = randint(1, max)
times = 0#记录本次游戏轮数
print 'Guess what i think?'
answer = input()
while answer!=num:
times += 1
if answer > num:
print 'too large'
用Python完成猜随机数的游戏
最新推荐文章于 2024-10-06 10:04:21 发布
本文介绍如何使用Python编程实现一个猜数字的小游戏。玩家需要猜测计算机生成的一个随机数,通过提示(过高或过低)来逐步接近正确答案,增强对Python随机数生成和条件判断的理解。
摘要由CSDN通过智能技术生成