While loop stopped even with use of continue


The script below is supposed to accept 8 letters maximum, and in case of input different from letter (for example an integer) a ValueError is raised and the while loop continue.
But when I run this part the program stops just after the error is raised, and the loop didn’t continue for the remaining input for iterated k.
Can you please help me figure out the reason?
Below the script and the output example (typing two letters and then a number)

import string

while k<8:
    l = input("Please type a letter: \n")
    lslettres = list(string.ascii_letters)
    if l not in lslettres:
             raise ValueError("You didn't type a letter! \n")
type or paste code here

Please type a letter:
Please type a letter:
Please type a letter:
Traceback (most recent call last):
File “E:\AT CTN IP\Projet Py\Jeu du pendu\pendu.py”, line 11, in
raise ValueError(“You didn’t type a letter! \n”)
ValueError: You didn’t type a letter!

Raised errors are supposed to stop everything – that’s their entire purpose. Perhaps you just want print("You didn't type a letter!") instead?

Also, the continue at the bottom of the loop is superfluous – all loops continue when they reach the bottom.

1 Like

the comiler only takes letter as an input and stops taking any input after you enter input other than letter. To eliminate this you should use fomented element.

Totally agree, it was unnecessarily complicated, I removed the continue and the raise and it is working perfectly. Thanks

Did you meant formating? Should I use the format in the print ?