Py学习  »  R4444  »  全部回复
回复总数  3
6 年前
回复了 R4444 创建的主题 » python中线性插值的逻辑误差

我想你在找这样的东西:

def interpolate(x, y, x_test):
    for i in range(len(x)):
        if x[i] > x_test:   #extrapolated condition: when the largest value of
            x_below = i - 1 #list x is greater than x_test
            x_above = i
            y_below = i - 1
            y_above = i
            continue # <---- I changed break to continue
        elif x[i] < x_test: #extrapolated condition: when the largest value of
            x_below = i + 1 #list x is greater than x_test
            x_above = i
            y_below = i + 1
            y_above = i
            continue # <---- I changed break to continue
        else:             #interpolated condition: when x_test lies between
            return y[i]    #two sample points.

    #a = (yabove - ybelow) / (xabove - xbelow)
    a = (y[y_above] - y[y_below]) / (x[x_above] - x[x_below])
    #b = ybelow - a * xbelow
    b = y[y_below] - a * x[x_below]
    #y’ = a * x’ + b
    return (a * x_test + b)

print(interpolate([1, 3, 5], [1, 9, 25], 5.0))

输出:

25

breaks continues

6 年前
回复了 R4444 创建的主题 » Python中游戏的Counter+1动作

检查的范围 ctr

你可以做的是:

全局定义ctr:

ctr = 0 #或者你想要的任何价值

然后在要重用的函数内部声明:

global ctr

因此,您的代码例如:

ctr = 0
class Player:
    def __init__(self, name):
        self.name = name
        self.hp = 40
        self.atk = 5
        self.sp = 10
        self.snk = 3 #FOR SNACKS ONLY
        self.lvl = 1
        self.exp = 0
    def attack(self, other):
        print("%s attacks %s for %d damage\n"%(self.name, other.name, self.a>
        other.takeDamage(self.atk, self)
    def takeDamage(self, dmg):
        print("%s takes %d damage\n" % (self.name, dmg))
        self.hp -= dmg
        #if(self.hp <= 0):
        #    self.die()
    #def die(self):
        #print("%s died" %(self.name))
        #print("-------GAMEOVER-------")
        #print("You earned %d exp this game" %(self.exp))
    def takeTurn(self, monsterArray):
        global ctr # <---- here
        print("%s HP:%d"%(self.name, self.hp))
        print("Atk:%d"%(self.atk))
        print("SP:%d"%(self.sp))
        print()
        #For every monster i, print information about it
        for i in monsterArray:
            #print information about Monster i
            i.printInfo()
        action = self.getAction()
        #Player selected "Attack"
        if(action == 1):
            print("\nChoose target: ")
            counter = 0
            for i in monsterArray:
                print("\n%d: " % (counter), end="")
                i.printInfo()
                counter += 1
            userInput = int(input(">"))

            self.attack(monsterArray[userInput])
            if(monsterArray[userInput].hp <= 0):
                monsterArray.pop(userInput)
        if(action == 2):
            print("\nYou are in Passive mode... - 1 dmg!\n")
            self.hp -= 1
        if(action == 3):
            print("\nYou are heal up to 3+ SP")
            self.hp +=  8
            ctr = ctr + 1 #count how many time used

    def getAction(self):
        global ctr # <---- and here
        while(1):
            print("\nAvailable actions:")
            print("1: Attack")
            print("2: Passive Mode [-1 dmg]")
            print("3: Snacks [%d out of 40]" % (ctr), end="")
            try:
                userInput = int(input(">"))
            except ValueError:
                print("Please enter a number")
                continue
            if(userInput < 1 or userInput > 3):
                print("invalid action number")
                continue
            elif(userInput == 2 and self.sp < 5):
                print("Loser! \n You are skipping yourself. Continue to figh>
                continue
            elif(userInput == 3 and self.hp< 40):
                print("NO MORE.... Use your health leftover!")
                continue
            return userInput

没有 global :

class Player:
    def __init__(self, name):
        self.name = name
        self.hp = 40
        self.atk = 5
        self.sp = 10
        self.snk = 3 #FOR SNACKS ONLY
        self.lvl = 1
        self.exp = 0
        self.ctr = 0
    def attack(self, other):
        print("%s attacks %s for %d damage\n"%(self.name, other.name, self.a))
        other.takeDamage(self.atk, self)
    def takeDamage(self, dmg):
        print("%s takes %d damage\n" % (self.name, dmg))
        self.hp -= dmg
        #if(self.hp <= 0):
        #    self.die()
    #def die(self):
        #print("%s died" %(self.name))
        #print("-------GAMEOVER-------")
        #print("You earned %d exp this game" %(self.exp))
    def takeTurn(self, monsterArray):
        print("%s HP:%d"%(self.name, self.hp))
        print("Atk:%d"%(self.atk))
        print("SP:%d"%(self.sp))
        print()
        #For every monster i, print information about it
        for i in monsterArray:
            #print information about Monster i
            i.printInfo()
        action = self.getAction()
        #Player selected "Attack"
        if(action == 1):
            print("\nChoose target: ")
            counter = 0
            for i in monsterArray:
                print("\n%d: " % (counter), end="")
                i.printInfo()
                counter += 1
            userInput = int(input(">"))

            self.attack(monsterArray[userInput])
            if(monsterArray[userInput].hp <= 0):
                monsterArray.pop(userInput)
        if(action == 2):
            print("\nYou are in Passive mode... - 1 dmg!\n")
            self.hp -= 1
        if(action == 3):
            print("\nYou are heal up to 3+ SP")
            self.hp +=  8
            self.ctr = self.ctr + 1 #count how many time used

    def getAction(self):
        while(1):
            print("\nAvailable actions:")
            print("1: Attack")
            print("2: Passive Mode [-1 dmg]")
            print("3: Snacks [%d out of 40]" % (self.ctr), end="")
            try:
                userInput = int(input(">"))
            except ValueError:
                print("Please enter a number")
                continue
            if(userInput < 1 or userInput > 3):
                print("invalid action number")
                continue
            elif(userInput == 2 and self.sp < 5):
                print("Loser! \n You are skipping yourself. Continue to figh")
                continue
            elif(userInput == 3 and self.hp< 40):
                print("NO MORE.... Use your health leftover!")
                continue
            return userInput
6 年前
回复了 R4444 创建的主题 » 用python测试flash消息的内容

正如@michaelbutscher所建议的,您可以:

assert b'Welcome back!' in response.data

这是因为 response.data 有一个 <class 'bytes'> 键入。所以,你需要匹配你的“欢迎回来!”字符串到字节。

在您的情况下,可以这样修复代码:

response = tester.post('login',
        data=dict(
        username='test', password='lol'), follow_redirects=True)
try:
    assert 'Welcome back!' in response.data
except AssertionError:
    print("Assertion failed!")

所以,对于响应数据= b"xxxxWelcome back!xxxx" 然后匹配,否则不匹配。

确保:字符串与输入(或要断言的字符串)完全匹配,并确保区分大小写在这里也很重要。