【问题标题】:Function call not working proparly (python)函数调用无法正常工作(python)
【发布时间】:2020-07-27 18:21:24
【问题描述】:

对于self.trying 的输入 1,set_jump() 方法不起作用。程序在其中执行while循环

代码:

jump = []
z = []


class jumps:
    def __init__(self, numberOfJumps, trying):
        self.numberOfJumps = numberOfJumps

        self.trying = int(trying)

    def setJumps(self):
        if self.trying == 1:
            # print("hello")
            self.set_jump()
        if self.trying == 2:
            self.get_jump()

        return ""

    def set_jump(self):

        counterSetJump = 0
        while counterSetJump < int(self.numberOfJumps):
            print("what was the distance the athlete jumped at jump number" + str(counterSetJump + 1))
            z.append(float(input()))
            counterSetJump += 1

        return ""

    def get_jump(self):
        counterGetJumps = 0
        while counterGetJumps < int(self.numberOfJumps):
            print(z[counterGetJumps])
            print("this is the jump for the " + str(counterGetJumps) + " time")
            counterGetJumps += 1
            return ""


class athlete:
    def __init__(self, name, yearsPro):
        self.name = name
        self.yearsPro = yearsPro

    def information(self):
        print("the athletes name is " + self.name)
        print("he as been active for " + str(self.yearsPro))
        return ""


a = []
b = []
i = 0
know = int(input("how many athletes you know the information for"))
while i < know:
    a.append(0)
    b.append(0)
    i = i + 1
j = 0
while j < know:
    a[j] = athlete(input("what is the athletes name?"), input("how many years as the athlete been active"))
    b[j] = jumps(input("how many jumps did the athlete jumped?"),
                 input("input 1 for settig and 2 for getting the jumps"))  # not getting called?
    b[j].setJumps()

    j = j + 1

infoFor = int(input("who do you want the info for"))
print(a[int(infoFor) - 1].information())
print(b[int(infoFor) - 1].get_jump())

【问题讨论】:

  • 您在这一行 print("what was the distance the athlete jumped at jump number" + i) 中添加了一个字符串和一个整数。它应该引发错误。例如,将其替换为 f"jump number {i}"jump number %d" %i。另外,您不需要使用while循环或手动递增,只需在程序中使用for i in range(int(know)):,在类中使用for i in range(self.numberOfJumps):即可。
  • 您的课程没有被调用,因为您试图通过列表递增并将其用作您的实例。这反过来又不允许激活b[i].setJumps() 命令。
  • 对不起,我不确定你的意思是什么,我应该改变什么? @Max

标签: python debugging methods


【解决方案1】:

检查代码

jump = []
z = []


class jumps:
    def __init__(self, numberOfJumps, trying):
        self.numberOfJumps = numberOfJumps
        self.trying = int(trying)
        print("trying = "+str(self.trying))

    def setJumps(self) :
        if self.trying == 1:
            #print("hello")
            self.set_jump()
        if self.trying == 2:
            self.get_jump()
        print("Yes I have chnaged trying to = :"+str(self.trying))
        return ""

    def set_jump(self):
        print("set_jump finally called")
        i = 0
        while(i < int(self.numberOfJumps)):
            print("what was the distance the athlete jumped at jump number" + str(i))
            z.append(int(input()))
            i = i + 1

    def get_jump(self):
        i = 0
        while(i < int(self.numberOfJumps)):
            return z[i]
            i = i + 1


class athlete:
    def __init__(self, name, yearsPro):
        self.name = name
        self.yearsPro = yearsPro
    def information(self):
        print("the athletes name is " + self.name)
        print("he as been active for " + str(self.yearsPro))
        return ""



a = []
b = []
i = 0
know = int(input("how many athletes you know the information for"))
while(i < know):
    a.append(0)
    b.append(0)
    i = i + 1
j = 0
while(j <know):
    a[j] = athlete(input("what is the athletes name?"), input("how many years as the athlete been active"))
    b[j] = jumps(input("how many jumps did the athlete jumped?"),
                 input("input 1 for settig and 2 for getting the jumps")) #not getting called?
    b[j].setJumps()
    k=j
    for k in b:
        k.setJumps()
        #j=j+1
    j = j + 1

infoFor = int(input("who do you want the info for"))
print(a[int(infoFor) - 1].information())
print(b[int(infoFor) - 1].setJumps())

【讨论】:

  • @osenter 这是我得到的输出 有多少运动员 你知道这些信息 1 运动员的名字是什么?d 运动员活跃了多少年 1 运动员跳了多少次?1输入 1 表示 settig,输入 2 表示跳跃 1 你想要谁的信息 1 运动员姓名是 d 他已经活跃了 1 年
  • @osenter 实际上在我的机器上运行得很好
  • 你描述你所得到的东西是我所得到的,这是一个问题。一旦我做了 setJumps,我希望它开始问我跳跃的距离是多少,而不是继续向谁提供信息。
  • @osenter 我对你的代码做了一些改动。现在检查一下,它应该可以根据您的需要工作。
猜你喜欢
  • 2019-09-13
  • 2019-03-05
  • 2012-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
相关资源
最近更新 更多