While loop stopped even with use of continue

Hi,

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

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

Please type a letter:
r
Please type a letter:
s
Please type a letter:
8
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 ?