samedi 27 mai 2017

Two very similar object.extend() codes to fill a list, one works one does not

The program below should put words in a list. The test for it to work is 'print(allwords[1])'. Funny thing is that the bottom part works, but when I use the top part it does not. The list apparently stays empty. How is what I am doing any different?

# DOES NOT WORK
print ('On what difficulty do you want to play?\n')
diffculty = input('1.Easy\n2.Medium\n3.Hard\n\n')
if diffculty == 1 or input == 'Easy' or input == 'easy' or input == 'EASY':
    with open('google-10000-english-usa-no-swears-short.txt') as inputfile:
        for line in inputfile:
            allwords.extend(line.strip().split(','))
elif diffculty == 2 or input == 'Medium' or input == 'medium' or input == 'MEDIUM':
    with open('google-10000-english-usa-no-swears-medium.txt') as inputfile:
        for line in inputfile:
            allwords.extend(line.strip().split(','))
elif diffculty == 3 or input == 'Hard' or input == 'hard' or input == 'HARD':
    with open('google-10000-english-usa-no-swears-long.txt') as inputfile:
        for line in inputfile:
            allwords.extend(line.strip().split(','))

# WORKS
print ('Okay then, let us begin with your first word.\n')
with open('google-10000-english-usa-no-swears-long.txt') as inputfile:
    for line in inputfile:
        allwords.extend(line.strip().split(','))

print (allwords[1])

Aucun commentaire:

Enregistrer un commentaire