-3

プログラムは反復しますが、特定のコード行までしか反復しません。次に、start 変数と stop 変数を 7 と 9 のように読み取り始めます?!?! ここで何が問題なのかわかりません。さらに奇妙なことに、7、9から48、51(開始値、停止値)にジャンプします。ここにコードの主要部分があります: http://pastebin.com/S0FZ3Jk7 そしてここに私がそれで使用しているデータがあります: http://pastebin.com/rchNJGBq

"""
Qualifiers_2.py

"""

#from Qualifier_classes import Qualifier

file_path = 'C:\\Users\\Neo\\My Documents\\Python Scripts\\FTC Scouting\\sample.txt'
file = open(file_path, 'r')

Data = []
all_teams = []
Teams_list = []
keys = ['Team Number: ', 'Name: ','Qualifier: ']
qualifier_keys = ['Qualifier: ', 'QP: ', 'RP: ', 'HS: ', 'Matches: ']
team_attr = ['name','number']
UI_options = [1,2]

class Qualifier(object):
    def __init__(self):
        self.name           = 'Qualifier Name'
        self.rp             = 0
        self.qp             = 0
        self.hs             = 0
        self.num_of_matches = 0
        self.data = [self.name,self.rp,self.qp,self.hs,self.num_of_matches]
    def __repr__(self):
        self.data = [self.name,self.rp,self.qp,self.hs,self.num_of_matches]
        return repr((self.data))

class Team:    
    def __init__(self,name,number):
        self.name = name
        self.number = number

    def __repr__(self):
        return repr((self.name,self.number))    

def list_teams(n):
    tTeams = []
    for line in Data:
        check = line.find(keys[n])
        if not check == -1:
            team = line.partition(keys[n])[2]
            if team not in tTeams:
                tTeams.append(team)
    return tTeams

#@param:    team_number , team to find
#@output:   gives back line number in raw Data list
def find_start_team(team_number):
    tcount = 0

    if str(team_number) in list_teams(0):
        for line in Data:
            if not line.find(str(team_number)) == -1:
                return tcount
            else:
                tcount += 1
    else:
        return 'not a valid team number'

#@param:    Start_team_number , the previous team next number
#@output:   the line number for the start and end of the team's acquired info
def find_team(start_team_number):
    try:        
        start_search = find_start_team(start_team_number)
        tcount = start_search
        for line in Data[start_search+1:]:
            if not line.find(keys[0]) == -1:
                return start_search, tcount
            else:
                tcount += 1
    except ValueError:
        return 'not a team number'

def team_data(start,stop):
    temp_data = []
    for line in Data[start:stop]:
        temp_data.append(line)
    return temp_data

#@param: qualifier_key , the name of the category you wish to find
#@param: data , the qualifier info for some team
#@output: returns back the data for the category 
def get_data(qualifier_key, data):
    for line in data:
        if not line.find(qualifier_key) == -1:
            return line.partition(qualifier_key)[2]

#@param: data , the qualifier data for some team
#@output: remaining_data , the remaining data after the first qualifier
#               note: will return -1 if there is no more qualifiers
def just_qualifiers(data):
    tcount = 0
    for line in data:
        if not line.find(qualifier_keys[0]) == -1:
            return data[tcount:]
        tcount += 1
    return -1

#@param: data , array of values for a single qualifier
def update_qualifier(data):
    tqual = Qualifier()
    tqual.name = data[0]
    tqual.rp = data[1]
    tqual.hs = data[2]
    tqual.num_of_matches = tdata[3]
    return tqual

#@param: data , the block of data for the team
#@output: the qualifier data and remainind data
def get_qualifier(data):
    temp_data = []
    for key in range(len(qualifier_keys)):
        temp_data.append(get_data(qualifier_keys[key],data))
    temp_qual = update_qualifier(temp_data)
    data = data[len(temp_data):]
    return temp_qual, data

#Turns our txt file into usable data
for line in file:
    line = line[:-1]
    Data.append(line)

#Creates a tuple for holding the ID of all the teams
all_teams.append(list_teams(0))
all_teams.append(list_teams(1))

#Makes an organized list of the ID for all the teams in a 2-tuple
for team in range(len(list_teams(0))):
    Teams_list.append(Team(all_teams[1][team],int(all_teams[0][team])))

"""*******************
NEW CODE
*******************"""

Qualifiers = []

for team in range(len(all_teams[0])):
    start , stop = find_team((all_teams[0][team]))

    tdata = []
    tqual = []


    #maps the specific teams block of data to tdata
    for line in team_data(start, stop):
        tdata.append(line)
    tdata = just_qualifiers(tdata)
    while True:
        if not just_qualifiers(tdata) == -1:
            tqual , tdata = get_qualifier(tdata)
            Qualifiers.append(tqual)        
        else:
            break    

print Qualifiers

"""*******************
END NEW CODE
*******************"""

file.close()

と私のトレースバック:

Traceback (most recent call last):
  File "C:/Users/Neo/Documents/Python Scripts/FTC Scouting/Qualifiers_3.py", line 152, in <module>
    if not just_qualifiers(tdata) == -1:
  File "C:/Users/Neo/Documents/Python Scripts/FTC Scouting/Qualifiers_3.py", line 96, in just_qualifiers
    for line in data:
TypeError: 'int' object is not iterable
4

2 に答える 2

0

just_qualifiersメソッドはリストまたは -1 を返します (yuk! なぜ空のリストではないのですか?) 。

一度呼び出して、値 -1 を取得し、それをtdata に代入し、すぐに次のようにjust_qualifiersAGAIN を呼び出します (なぜですか?) 。if not just_qualifiers(tdata) == -1:if not just_qualifiers(-1) == -1:for line in -1:

修飾子がない場合に空のリストを返すように、その関数を書き直すことを強くお勧めします。他の同様の機能も同様です。空のリストを反復することは非常に安全です。つまり、が空のfor line in data:場合は何もしませんdata。空かどうかを確認する必要がある場合は、必要に応じてif data:orif not data:を実行できます。

于 2012-04-09T05:38:59.993 に答える
0

これを理解するためにプログラム全体を確認するつもりはありませんが、トレースバックの一部として、一目で 1 つのことがわかりました。

def update_qualifier(data):
    tqual = Qualifier()
    tqual.name = data[0]
    tqual.rp = data[1]
    tqual.hs = data[2]
    tqual.num_of_matches = tdata[3]
    return tqual

tdata[3]ローカル変数ではなく、ここでグローバル変数を参照するつもりでしたdataか?

于 2012-04-09T03:09:18.583 に答える