Validating a users input
我对python还比较陌生,我使用了一个while-true循环。在那个循环里我有
1 | Sentence= input("please enter a sentence").lower().split() |
但是,我希望创建验证,以便在用户输入数字而不是字母时出现错误消息。我正在研究并看到了.isalpha的用法。有没有人会在whie-true循环中创建和错误消息来输入数字?
1 2 3 4 | sentence = input("please enter a sentence").lower().split() for word in sentence: if not word.isalpha(): print("The word %s is not alphabetic." % word) |
像这样?
1 2 3 4 | sentence = input("please enter a sentence:").lower().split() while False in [word.isalpha() for word in sentence]: sentence = input("Do not use digits. Enter again:").lower().split() |
号