设为首页收藏本站

Crossin的编程教室

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

集中答疑专用贴

  [复制链接]

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
>>>
回复

使用道具 举报

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

273#
发表于 2018-1-19 21:45:47 |只看该作者
TED 发表于 2018-1-19 17:00
先看报错原因,NameError: name 'times' is not defined,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
回复

使用道具 举报

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是记录每局游戏猜了几次
回复

使用道具 举报

6

主题

2

好友

654

积分

实习版主

Rank: 7Rank: 7Rank: 7

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

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

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

使用道具 举报

0

主题

0

好友

33

积分

新手上路

Rank: 1

277#
发表于 2018-1-21 10:50:50 |只看该作者
老师好,

今天在46课查天气里有个问题,我的代码如下,输出content1的时候看到一堆奇怪的东西,没有全部省份的代码,这是为什么?谢
回复

使用道具 举报

0

主题

0

好友

33

积分

新手上路

Rank: 1

278#
发表于 2018-1-21 10:54:31 |只看该作者
奇怪,回复下半部分怎么没了?重发一次:
代码:
import urllib2
url1='http://m.weather.com.cn/data5/city.xml'
content1=urllib2.urlopen(url1).read()
provinces=content1.split(',')
url = 'http://m.weather.com.cn/data3/city%s.xml'
print content1
--------------------------------------------
输出:
<html>
<head>
</head>
<body>
<script type="text/javascript">
        window.onload = function() {
        window.open("/","_self");
        };
        </script>
<!-- START WRating v1.0 -->
<script type="text/javascript" src="http://c.wrating.com/a1.js">
</script>
<script type="text/javascript">
var vjAcc="860010-2151010100";
var wrUrl="http://c.wrating.com/";
vjTrack("");
</script>
<noscript><img src="http://c.wrating.com/a.gif?a=&c=860010-2151010100" width="1" height="1"/></noscript>
<!-- END WRating v1.0 -->
<script type="text/javascript">
window._wat = window._wat || [];
(function() {
    var wm = document.createElement("script");
    wm.src = "http://analyse.weather.com.cn/js/v1/wa.js?site_id=1";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(wm, s);
})();
</script>
</body>
</html
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

279#
发表于 2018-1-21 23:16:01 |只看该作者
刚卓 发表于 2018-1-21 10:54
奇怪,回复下半部分怎么没了?重发一次:
代码:
import urllib2

data5 改 data3,第一个帖子开头有说
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

33

积分

新手上路

Rank: 1

280#
发表于 2018-1-22 12:38:30 |只看该作者
crossin先生 发表于 2018-1-21 23:16
data5 改 data3,第一个帖子开头有说

谢谢先生!出来了,但为什么要改成data3?代码中的URL为何都是没网页的?还有,对于每个城市,抓取地区列表的url是什么?

还有,对于每个省,抓取城市列表的代码(如下),第14行是否可有可无,因为输出的城市和城市代码总是用‘,’分开的。

# -*- coding: utf-8 -*-
import urllib2
url1='http://m.weather.com.cn/data3/city.xml'   
content1=urllib2.urlopen(url1).read()
provinces=content1.split(',')
url = 'http://m.weather.com.cn/data3/city%s.xml'
print content1

url2='http://m.weather.com.cn/data3/city%s.xml'
for p in provinces:
    p_code=p.split('|')[0]
    url2=url%p_code
    content2=urllib2.urlopen(url2).read()
    cities=content2.split(',')
    print content2
----------------------------------------
回复

使用道具 举报

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

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

GMT+8, 2024-4-28 01:59 , Processed in 0.025660 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部