鱼C论坛

 找回密码
 立即注册
查看: 827|回复: 3

课后练习乌龟吃鱼,体力值没有掉的问题

[复制链接]
发表于 2019-1-19 20:01:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
先放我的代码:

import random

'''build a 10x10 site'''
site=[(x,y) for x in range(0,11) for y in range(0,11)]

'''global locations'''
fishlist=list(random.sample(site,10))
tor_loc=list(random.sample(site,1))[0]
tortose_health=100
Round=0

'''build the object'''
class fish:
    def __init__(self,location,fish=True):
        if fish:
            self.movement=[(-1,0),(1,0),(0,-1),(0,1)]
        else:
            self.movement=[(-1,0),(1,0),(0,-1),(0,1),(-2,0),(2,0),(0,-2),(0,2)]
        self.location=location  
           
    def update_location(self):
        move=list(random.sample(self.movement,1))[0]
        new_loc=[]
        for i in range(0,2):
            new=move[i]+self.location[i]
            if new>10 or new<0:
                new+= -2*move[i]
            else:
                pass
            new_loc.append(new)
        new_loc=tuple(new_loc)
        return new_loc

'''health calculate'''
def health_cal(tor_loc,fishlist,tortose_health):
    tortose_health=tortose_health-1
    for thisfish in fishlist:
        if thisfish==tor_loc:
            fishlist.remove(thisfish)
            tortose_health+=20
            if tortose_health>100:
                tortose_health=100
        else:
            pass
    return tortose_health, fishlist


'''is alive?'''
def is_alive(fishlist,tortose_health):
    if len(fishlist)<1 or tortose_health<1:
        return False
    else:
        return True


'''update one time'''
while is_alive(fishlist,tortose_health):
    for cache in fishlist:
        thisfish=fish(cache)
        new_loc=thisfish.update_location()
        fishlist[fishlist.index(cache)]=new_loc

    tortose=fish(tor_loc,fish=False)
    tor_loc=tortose.update_location()
    health_cal(tor_loc,fishlist,tortose_health)
    Round+=1
    print ('---------------Round%s----------------------'%(int(Round)))
    print ('-----------Tortose\'s health%s--------------'%(int(tortose_health)))
    print (fishlist)

print ('----------GAME OVER!---------------')
if len(fishlist)<1:
    print ('-------------------TORTOSE WIN!-------------------')
    print ('tortose health:',tortose_health)
else:
    print ('-------------------FICHES WIN!--------------------')






我感觉我在调用healt_cal()这个功能的时候已经在第一行先减了1点体力值额,为什么我打印出来的结果体力值一直是100额。哪位大神救救本宝宝额

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-19 20:27:50 | 显示全部楼层
函数内部变量和全局变量不在同一级。

你外面定义了全局变量,不影响函数内部变量。

也就是说,你在函数内部修改的只是局部变量,修改不了外面的全局变量。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-19 20:31:08 | 显示全部楼层
heidern0612 发表于 2019-1-19 20:27
函数内部变量和全局变量不在同一级。

你外面定义了全局变量,不影响函数内部变量。

我刚才试了一下,好像return完了根本那个值就没变。是不是意思是我return完了还得再把return出来的东西赋给一个标签才行啊~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-19 20:37:17 | 显示全部楼层
本帖最后由 heidern0612 于 2019-1-19 20:54 编辑
guili930000 发表于 2019-1-19 20:31
我刚才试了一下,好像return完了根本那个值就没变。是不是意思是我return完了还得再把return出 ...


你为啥形参和实参用的名字都一样呢?

不用贴什么标签,调用函数的时候,自动就返回return的值了。



另外不要想着用nonlocal修改全局变量,因为你下面乌龟体力不只是个全局变量,还是个参数。

如果用nonlocal的话,还是会报错。

总之,这个问题比较复杂,还是想办法封装到类里面最好。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-30 16:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表