设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 3977|回复: 5
打印 上一主题 下一主题

这是什么错误,怎么改!!!

[复制链接]

7

主题

0

好友

103

积分

注册会员

Rank: 2

跳转到指定楼层
楼主
发表于 2019-12-18 22:27:12 |只看该作者 |倒序浏览
python基础41课!!

用文件保存游戏3.png (216.28 KB, 下载次数: 230)

用文件保存游戏3.png

回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

沙发
发表于 2019-12-19 15:23:19 |只看该作者
12行
score = [0,0,0]

这里是需要一个列表,所以是中括号
大括号就变成集合了,是没法索引取值的
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

7

主题

0

好友

103

积分

注册会员

Rank: 2

板凳
发表于 2019-12-19 22:32:42 |只看该作者
from random import randint
name = input('请输入你的名字:')#输入玩家名字
f =open('D:\py\游戏.txt')
lines = f.readlines()
f.close()
scores = {}
for l in lines:
    s = l.split()
    scores[s[0]]= s[1:]
    score = scores.get(name)
    if score is None:
        score =[0,0,0]
game_times =int(score[0])
min_times =int(score[1])
tatal_times =int(score[2])
if game_times > 0:
    avg_times = float(tatal_times)/game_times
else:
    avg_times =0
print(' %s  你已经玩了%d次,最少%d轮猜出答案,平均%.2f轮猜出答案' % (name,game_times, min_times, avg_times))
num=randint(0,100)
times = 0     #记录本次游戏轮数
print ('guess what i think?')
bingo=False
while bingo==False:
    times +=1   #记录游戏轮数
    answer=eval(input())
    if answer>num:
        print ('too big.')
    if answer<num:
        print ('too small')
    if answer==num:
        print ('bingo.')
        bingo=True
f.close()
if game_times ==0 or times < min_times:
    min_times = times
tatal_times +=times
game_times +=1
scores[name] = [str(game_times),str(min_times),str(tatal_times)]
result = ''
for n in scores:
    line = n + ''+  ''.join(scores[n]) + '\n'
    result +=line
f = open('D:\py\游戏.txt','w')
f.write(result)
f.close()


老师,能解释下为什么跑成这样么? 练习1.png 练习2.png
回复

使用道具 举报

7

主题

0

好友

103

积分

注册会员

Rank: 2

地板
发表于 2019-12-19 22:35:07 |只看该作者
六次猜对结果
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

5#
发表于 2019-12-20 16:12:06 |只看该作者
落梦 发表于 2019-12-19 22:35
六次猜对结果

line = n + ' '+  ' '.join(scores[n]) + '\n'
这句里是空格,不是空字符。所以你的结果都连到一起了,没有空格了
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

7

主题

0

好友

103

积分

注册会员

Rank: 2

6#
发表于 2019-12-20 21:59:37 |只看该作者
嗯嗯,谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-4-19 12:11 , Processed in 0.019827 second(s), 24 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部