Bykaugan
Bykaugan

Reputation: 99

Print statements won't execute after the for loop

"""
ID: kunalgu1
LANG: PYTHON3
TASK: ride
"""
fin = open ('ride.in', 'r')

fout = open ('ride.out', 'w')
lines = fin.readlines()

cometString = lines[0]
cometValue = 1

groupString = lines[1]
groupValue = 1

def orderS (val):
    arrL = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] 
    arrN = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
    indexVal = arrL.index(val.lower())
    return arrN[indexVal]

for x in cometString:
    print(orderS(x))
    cometValue *= orderS(x)
    print(cometValue)

Here is the main error: it won't print

cometValue = cometValue % 47
print(cometValue)

fout.close()

Upvotes: 0

Views: 57

Answers (1)

Barmar
Barmar

Reputation: 780663

The loop gets an error because the lines returned by readlines() include the newline terminator. When it calls orderS() for that character, arrL.index() fails because there's no newline in arrL.

You can remove the newline with the rstrip() method:

cometString = lines[0].rstrip()

You could also have orderS() return a default value when the character can't be found:

def orderS (val):
    arrL = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] 
    arrN = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]
    try:
        indexVal = arrL.index(val.lower())
        return arrN[indexVal]
    except ValueError:
        return 27

Upvotes: 1

Related Questions