【发布时间】: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