简体   繁体   中英

Spelling test check not functioning (python)

Very simple error that I've had for quite a while now that I seem to be missing, and it's really bugging me

studentspelling = input ('Spelling: ')
spellingnumber = y6wordlist.index(studentspelling)
if studentspelling == y6wordlist[spellingnumber]:
    print ('Correct. Awarded 2 points.')
    total = total + 2
else:
    print ('Incorrect answer. Awarded no points.')
    total = total

However when I try to input a word during the spelling test that is NOT the word assigned to y6wordlist[spellingnumber] I get this error:

ValueError: 'dgfh' is not in list

now obviously I know that it's not in the list, but none of my code has said that it has to be, or at least I don't think so. I have tried a number of things such as using 'elif' but to no degree of success. Any ideas of what I'm doing wrong?

The index method will raise a ValueError if the value is not in the list:

>>> a = [1,2,3]
>>> a.index(1)
0
>>> a.index(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 4 is not in list
>>>

You can test whether the word is in the list using in :

if word in mylist:
    i = mylist.index(word)
    # do something
else:
    # do something different

The statement:

spellingnumber = y6wordlist.index(studentspelling)

will result in a ValueError if the word is not in the list

** incidentally, if you raise the error, you automatically know the word isn't right**

You could use:

try:
    spellingnumber = y6wordlist.index(studentspelling)
    # and you could just go straight to this
    print ('Correct. Awarded 2 points.')
    total = total + 2

except ValueError: 
    print ('Incorrect answer. Awarded no points.')

or something similar

index() raises a ValueError when the argument ( studentspelling ) is not in the list.

If you have (or create) a variable that keeps track of the current question then I think you meant to write:

current_question = 0
student_spelling = input ('Spelling: ')
if student_spelling == y6wordlist[current_question]:
    print ('Correct. Awarded 2 points.')
    total = total + 2
else:
    print ('Incorrect answer. Awarded no points.')
    total = total

current_question = current_question + 1

This does: give the spelling of the current question/word. Then, lookup the correctly spelling word up at position current_question in y6wordlist and see if it's the same as student_spelling . If so then the student has spelled the word correctly and otherwise not.

The docs for the list data structure shows:

list.index(x): Return the index in the list of the first item whose value is x. It is an error if there is no such item.

Since you're preferring to test for membership, rather than get the index number of a value that may not be present, you might instead do:

if studentspelling in y6wordlist

Alternatively, you could also have a try/catch block:

try:
    spellingnumber = y6wordlist.index(studentspelling)
    print ('Correct. Awarded 2 points.')
    total = total + 2
except ValueError:
    print ('Incorrect answer. Awarded no points.')
    total = total

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM