为什么我可以在调试时运行此代码,但不能正常运行?

问题描述 投票:0回答:1

我正在开发一个学校项目,我能够在调试模式下完美运行此代码,但在正常运行时出现错误。

import linecache

# Find if senators from the same state are in the same party
def Same_State(p1, p2):
    if(p1 == p2):
        isSame = True # Set to true if parties match
    else:
        isSame = False # Set false if no match found

    return isSame

# Find senators in same state
def Find_Sens(sList, c):
    index2 = 0 # Used to store values in fList

    fList = [] # Empty list to hold found senators

    for index in range(0, len(sList) + 1):
        if(index2 == 2):
            return fList # Return list if both senators found
        
        line = str(sList[index]).split(',')

        match c:
            case 1:
                if(line[1] == 'Alabama'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 2:
                if(line[1] == 'Alaska'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 3:
                if(line[1] == 'Arizona'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 4:
                if(line[1] == 'Arkansas'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 5:
                if(line[1] == 'California'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 6:
                if(line[1] == 'Colorado'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 7:
                if(line[1] == 'Connecticut'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 8:
                if(line[1] == 'Delaware'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 9:
                if(line[1] == 'Florida'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 10:
                if(line[1] == 'Georgia'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 11:
                if(line[1] == 'Hawaii'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 12:
                if(line[1] == 'Idaho'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 13:
                if(line[1] == 'Illinois'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 14:
                if(line[1] == 'Indiana'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 15:
                if(line[1] == 'Iowa'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 16:
                if(line[1] == 'Kansas'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 17:
                if(line[1] == 'Kentucky'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 18:
                if(line[1] == 'Louisiana'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 19:
                if(line[1] == 'Maine'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 20:
                if(line[1] == 'Maryland'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 21:
                if(line[1] == 'Massachusetts'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 22:
                if(line[1] == 'Michigan'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 23:
                if(line[1] == 'Minnesota'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 24:
                if(line[1] == 'Mississippi'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 25:
                if(line[1] == 'Missouri'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 26:
                if(line[1] == 'Montana'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 27:
                if(line[1] == 'Nebraska'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 28:
                if(line[1] == 'Nevada'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 29:
                if(line[1] == 'New Hampshire'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 30:
                if(line[1] == 'New Jersey'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 31:
                if(line[1] == 'New Mexico'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 32:
                if(line[1] == 'New York'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 33:
                if(line[1] == 'North Carolina'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 34:
                if(line[1] == 'North Dakota'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 35:
                if(line[1] == 'Ohio'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 36:
                if(line[1] == 'Oklahoma'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 37:
                if(line[1] == 'Oregon'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 38:
                if(line[1] == 'Pennsylvania'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 39:
                if(line[1] == 'Rhode Island'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 40:
                if(line[1] == 'South Carolina'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 41:
                if(line[1] == 'South Dakota'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 42:
                if(line[1] == 'Tennessee'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 43:
                if(line[1] == 'Texas'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 44:
                if(line[1] == 'Utah'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 45:
                if(line[1] == 'Vermont'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 46:
                if(line[1] == 'Virginia'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 47:
                if(line[1] == 'Washington'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 48:
                if(line[1] == 'West Virginia'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 49:
                if(line[1] == 'Wisconsin'):
                    fList.append(sList[index])
                    index2 = index2 + 1
            case 50:
                if(line[1] == 'Wyoming'):
                    fList.append(sList[index])
                    index2 = index2 + 1

# Grab lines from file
def Line_Grab():
    empList = [] # Empty list

    with open('Senate114.txt', 'r') as sen14:
        for index in range(0, len(sen14.readlines()) + 1):
            line = linecache.getline('Senate114.txt', index) # Read lines from file
            empList.append(line) # Store lines in list
        return empList # Return completed list

# Main Function
count = 1 # Used for match case in Find_Sens

sameSen = 0 # Stores senator data

senList = Line_Grab() # Grab lines from file

del senList[0] # Delete first element in list

while(count <= 50):
    twoSens = Find_Sens(senList, count) # Find senators in each state
    
    # Split strings into component parts
    senator1 = str(twoSens[0]).split(',')
    senator2 = str(twoSens[1]).split(',')

    found = Same_State(senator1[2], senator2[2]) # Determine if senators have same affiliation

    if(found == True):
        sameSen = sameSen + 1 # Increment
    
    count = count + 1 # Increment

print('Number of states with senators of the same party affiliation: ' + str(sameSen))

该错误具体发生在第 22 行。我收到一个错误,因为我的索引超出范围。

line = str(sList[index]).split(',')

这里是Senate114.txt 文件的信息,以备需要时使用。

Al Franken,Minnesota,D
Amy Klobuchar,Minnesota,D
Angus King,Maine,I
Barbara Boxer,California,D
Barbara Mikulski,Maryland,D
Ben Sasse,Nebraska,R
Benjamin Cardin,Maryland,D
Bernard Sanders,Vermont,I
Bill Cassidy,Louisiana,R
Bill Nelson,Florida,D
Bob Corker,Tennessee,R
Brian Schatz,Hawaii,D
Charles Schumer,New York,D
Chris Coons,Delaware,D
Chris Murphy,Connecticut,D
Chuck Grassley,Iowa,R
Claire McCaskill,Missouri,D
Cory Booker,New Jersey,D
Cory Gardner,Colorado,R
Dan Coats,Indiana,R
Dan Sullivan,Alaska,R
David Perdue,Georgia,R
David Vitter,Louisiana,R
Dean Heller,Nevada,R
Deb Fischer,Nebraska,R
Debbie Stebenow,Michigan,D
Dianne Feinstein,California,D
Edward Markey,Massachusetts,D
Elizabeth Warren,Massachusetts,D
Gary Peters,Michigan,D
Harry Reid,Nevada,D
Heidi Heitkamp,North Dakota,D
Jack Reed,Rhode Island,D
James Inhofe,Oklahoma,R
James Lankford,Oklahoma,R
Jeanne Shaheen,New Hampshire,D
Jeff Flake,Arizona,R
Jeff Merkley,Oregon,D
Jeff Sessions,Alabama,R
Jerry Moran,Kansas,R
Jim Risch,Idaho,R
Joe Donnelly,Indiana,D
Joe Manchin,West Virginia,D
John Barrasso,Wyoming,R
John Boozman,Arkansas,R
John Cornyn,Texas,R
John Hoeven,North Dakota,R
John McCain,Arizona,R
John Thune,South Dakota,R
John Walsh,Montana,D
Johnny Isakson,Georgia,R
Joni Ernst,Iowa,R
Kelly Ayotte,New Hampshire,R
Kirsten Gillibrand,New York,D
Lamar Alexander,Tennessee,R
Lindsey Graham,South Carolina,R
Lisa Murkowski,Alaska,R
Marco Rubio,Florida,R
Maria Cantwell,Washington,D
Mark Kirk,Illinois,R
Mark Warner,Virginia,D
Martin Heinrich,New Mexico,D
Mazie Hirona,Hawaii,D
Michael Bennet,Colorado,D
Michael Enzi,Wyoming,R
Mike Crapo,Idaho,R
Mike Lee,Utah,R
Mike Rounds,South Dakota,R
Mitch McConnell,Kentucky,R
Orrin Hatch,Utah,R
Pat Roberts,Kansas,R
Pat Toomey,Pennsylvania,R
Patrick Leahy,Vermont,D
Patty Murray,Washington,D
Rand Paul,Kentucky,R
Richard Blumenthal,Connecticut,D
Richard Burr,North Carolina,R
Richard Durbin,Illinois,D
Richard Shelby,Alabama,R
Rob Portman,Ohio,R
Robert Casey,Pennsylvania,D
Robert Menendez,New Jersey,D
Roger Wicker,Mississippi,R
Ron Johnson,Wisconsin,R
Ron Wyden,Oregon,D
Roy Blunt,Missouri,R
Sheldon Whitehouse,Rhode Island,D
Shelly Capito,West Virginia,R
Sherrod Brown,Ohio,D
Steve Daines,Montana,R
Susan Collins,Maine,R
Tammy Baldwin,Wisconsin,D
Ted Cruz,Texas,R
Thad Cochran,Mississippi,R
Thom Tillis,North Carolina,R
Thomas Carper,Delaware,D
Tim Kaine,Virginia,D
Tim Scott,South Carolina,R
Tom Cotton,Arkansas,R
Tom Udall,New Mexico,D

我尝试调试代码,但是,如前所述,代码似乎在调试模式下完成得很好。输出也正确,有 35 个州有同一党派的参议员。

python
1个回答
0
投票

好吧,所以我必须出去一段时间。当我回来时,我启动了计算机并打开了项目,现在它运行得很好。我不知道为什么之前会出现错误,我也不知道为什么现在已经修复了。我只能假设 VS Code 发生了一些奇怪的事情。感谢任何回复的人。

© www.soinside.com 2019 - 2024. All rights reserved.