设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

集中答疑专用贴

  [复制链接]

6

主题

2

好友

654

积分

实习版主

Rank: 7Rank: 7Rank: 7

276#
发表于 2018-1-20 12:01:21 |只看该作者
刚卓 发表于 2018-1-20 11:31
老师好,

我在41课(用文件保存游戏3)里有几个问题:1.不管猜几次,文档里的记录次数总是比猜的次数多一 ...

如果是第一次玩,min_times就等于这次玩的次数;之后的话会比较 更新最小的
回复

使用道具 举报

6

主题

2

好友

654

积分

实习版主

Rank: 7Rank: 7Rank: 7

275#
发表于 2018-1-20 11:59:27 |只看该作者
刚卓 发表于 2018-1-20 11:31
老师好,

我在41课(用文件保存游戏3)里有几个问题:1.不管猜几次,文档里的记录次数总是比猜的次数多一 ...

while bingo ==False:中应该是times+=1 不是game_times+=1
game_times是记录玩了几局游戏,times是记录每局游戏猜了几次
回复

使用道具 举报

0

主题

0

好友

33

积分

新手上路

Rank: 1

274#
发表于 2018-1-20 11:31:43 |只看该作者
老师好,

我在41课(用文件保存游戏3)里有几个问题:1.不管猜几次,文档里的记录次数总是比猜的次数多一次。 2. 文档里第二列(最少猜出答案轮数)和第三列(平均猜出答案轮数)总是0. 3. 保存游戏记录的文档是不是只能用txt?我试过用docx,结果不行。4. 代码第42行和第43行:if game_times==0 or times<min_times:
    min_times=times
在第40课的解释是#如果是第一次玩,或者轮数比最小轮数少,则更新最小轮数。这句话是否这样理解:假如A是第一次玩,则文档自动记录一个新数据,若B是第3次玩,且用了比前两次更少的轮数猜出答案,则更新B的最少轮数?谢谢!
# -*- coding: utf-8 -*-
from random import randint

name=raw_input('your name please: ')

f=open('/Users/Max/Desktop/tires/game.txt')
lines=f.readlines()
f.close()

scores={} #inicialization de diccionario vacio

for l in lines:
    s=l.split() #make each row of data as list
    scores[s[0]]=s[1:] #first term is key, rest are values
score=scores.get(name) #search current player's data
if score is None:
    score=[0, 0, 0]
   
game_times=int(score[0])
min_times=int(score[1])
total_times=int(score[2]) #depositar a cada variables
if game_times > 0:
    avg_times=float(total_times)/game_times #calculo
else:
    avg_times=0
print'you have played %d round, you got answer at least %d round,average round is %.2f'%(game_times, min_times, avg_times)
   
num=randint(1,10)
times=0
print'Guess number'
bingo=False
while bingo==False:
    game_times +=1 #ronda mas 1
    answer=input()
    if answer<num:
        print'too small'
    if answer>num:
        print'too big'
    if answer==num:
        print'bingo!'
        bingo=True
if game_times==0 or times<min_times:
    min_times=times # if it's first time or round is less than least round, then update the least round
total_times+=times #total round of game is increasing
game_times+=1 #game time is increasing

scores[name ]=[str(game_times),  str(min_times),  str(total_times)] #update score to determined player,str for reformation
result='' #inicializing empty string for data deposit purpose
for n in scores:
    line=n+ ' ' + ' ' .join (scores[n])+'\n'
    result+=line #agregando resultado
   
f=open('/Users/Max/Desktop/tires/game.txt','w')
f.write(result)
f.close()
-------------------------------------------------------
输出
your name please: Luo
you have played 0 round, you got answer at least 0 round,average round is 0.00
Guess number
6
bingo!
>>>
================= RESTART: /Users/Max/Desktop/tires/game.py =================
your name please: A
you have played 0 round, you got answer at least 0 round,average round is 0.00
Guess number
6
too small
8
too small
9
too small
10
bingo!
>>>
================= RESTART: /Users/Max/Desktop/tires/game.py =================
your name please: A
you have played 5 round, you got answer at least 0 round,average round is 0.00
Guess number
6
too small
9
too small
10
bingo!
>>>
------------------------------------------------------------
文档显示
A 9 0 0
Han 10 0 0
Luo 2 0 0
回复

使用道具 举报

0

主题

0

好友

33

积分

新手上路

Rank: 1

273#
发表于 2018-1-19 21:45:47 |只看该作者
TED 发表于 2018-1-19 17:00
先看报错原因,NameError: name 'times' is not defined,times没有定义,那接下来查你代码对times是不是 ...

谢谢你!
回复

使用道具 举报

6

主题

2

好友

654

积分

实习版主

Rank: 7Rank: 7Rank: 7

272#
发表于 2018-1-19 17:00:12 |只看该作者
刚卓 发表于 2018-1-19 10:57
请教高人:小可在微信40课(用文件保存游戏2)上碰到一点问题。
我和教程图片上写得一样,为何会出现‘time ...

先看报错原因,NameError: name 'times' is not defined,times没有定义,那接下来查你代码对times是不是漏了定义就好了;

微信40课图片中,num=randint(1,100)这句之后,有个times=0
回复

使用道具 举报

0

主题

0

好友

33

积分

新手上路

Rank: 1

271#
发表于 2018-1-19 10:57:01 |只看该作者
请教高人:小可在微信40课(用文件保存游戏2)上碰到一点问题。
我和教程图片上写得一样,为何会出现‘times'没有定义?谢谢
# -*- coding: utf-8 -*-
from random import randint
f=open('/Users/Max/Desktop/tires/game.txt')
score=f.read().split() #extraer datos desde carpeta
f.close()
game_times=int(score[0])
min_times=int(score[1])
total_times=int(score[2]) #depositar a cada variables
if game_times > 0:
    avg_times=float(total_times)/game_times #calculo
else:
    avg_times=0
print'you have played %d round, you got answer at least %d round,average round is %.2f'%(game_times, min_times, avg_times)
   
num=randint(1,10)
print'Guess number'
bingo=False
while bingo==False:
    game_times +=1 #ronda mas 1
    answer=input()
    if answer<num:
        print'too small'
    if answer>num:
        print'too big'
    if answer==num:
        print'bingo!'
        bingo=True
if game_times==0 or game_times<min_times:
    min_times=times # if it's first time or round is less than least round, then update the least round
total_times+=times #total round of game is increasing
game_times+=1 #game time is increasing
result='%d %d %d' % (game_times, min_times, total_times)
f=open('/Users/Max/Desktop/tires/game.txt','w')
f.write(result)
f.close()
-------------------------------------------------------------------------

================= RESTART: /Users/Max/Desktop/tires/game.py =================
you have played 2 round, you got answer at least 3 round,average round is 2.00
Guess number
2
too small
5
too small
7
too big
6
bingo!

Traceback (most recent call last):
  File "/Users/Max/Desktop/tires/game.py", line 30, in <module>
    total_times+=times #total round of game is increasing
NameError: name 'times' is not defined
>>>
回复

使用道具 举报

0

主题

0

好友

33

积分

新手上路

Rank: 1

270#
发表于 2018-1-19 10:54:22 |只看该作者
请教高人:小可在微信40课(用文件保存游戏2)上碰到一点问题。


我和教程图片上写得一样,为何会出现‘times'没有定义?谢谢
回复

使用道具 举报

6

主题

0

好友

116

积分

注册会员

Rank: 2

269#
发表于 2018-1-13 04:05:51 |只看该作者
TED 发表于 2018-1-12 19:06
第一个问题
字符串“\bs\S*?e\b”如果不处理,转义字符就会发挥作用,导致字符串成了“s\S*?”,按这个来 ...

谢谢 ted的回答!! 另外关于问题2, 你可能这样改不是用于命令行调用 python2/3 吗, 对scripts里面的 #! python3 有效果吗?  由此衍生一个问题 #之后不是被注释掉吗 为什么 # -*- coding: utf-8 -*- 会改变编码方式呢?
回复

使用道具 举报

6

主题

2

好友

654

积分

实习版主

Rank: 7Rank: 7Rank: 7

268#
发表于 2018-1-12 19:06:21 |只看该作者
pylearner 发表于 2018-1-11 15:50
ted老师好! 有三个小问题请教一下
1. 没有明白: raw完全匹配 '' 里面包含的字符串,例如r'\t'是匹配文本中 ...

第一个问题
字符串“\bs\S*?e\b”如果不处理,转义字符就会发挥作用,导致字符串成了“s\S*?”,按这个来进行正则表达式是不对的吧? 那我加个r“\bs\S*?e\b” 保证正则的时候是按着“\bs\S*?e\b”来,其中 \b \S是正则表达式中的代表,就不是转义字符了
第二个问题
我的个人解决方案是,把python2的python.exe改成python2.exe,运行py2时 我是运行python2就行了
第三个问题
同求答案。。
回复

使用道具 举报

6

主题

0

好友

116

积分

注册会员

Rank: 2

267#
发表于 2018-1-11 15:50:05 |只看该作者
本帖最后由 pylearner 于 2018-1-11 18:09 编辑
TED 发表于 2018-1-10 17:02
我不是老师  不敢当哈

ted老师好! 有三个小问题请教一下
1. 没有明白: raw完全匹配 '' 里面包含的字符串,例如r'\t'是匹配文本中的\t.那么为什么在找单词的时候前面会加上raw呢?那样不是匹配包含转义符的字符串吗?
python 3.63 os win10
#! python3
import re
text = 'site sea sue sweet see case sse se ssee ssssse loses'
x = re.findall( r"\bs\S*?e\b", text)

2. 按照我的系统win10安装了Python2.7 & 3.64 安装和环境都是正常配置好.(调用 py -3 和 py -2 都可以).但是在scripts前面加 #! /usr/bin/env python2 或者 #!python2 等都是启动python3.64. 怎么样写才能启动python2?
eg:
#! /usr/bin/env python2import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))

输出:
hello from Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
请问怎么办? 我没用其他ide试过.

问题三: 请问如何在def函数内定义key值为数字的字典

def printAll(**kargs):
    for k in kargs:
        print (k, ':', kargs[k])
        print (kargs)
printAll(a=1, b=(1,2), c=3,4 = 5)

各种改法都报错 keyword cant be an expression.

回复

使用道具 举报

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

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

GMT+8, 2024-5-18 10:49 , Processed in 0.019408 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部