I have a piece of code that asks for an input. The input must not contain any numbers:
def main():
invalid = []
foo = ""
foo = str(input("Enter a word: ") )
invalid = ["1","2","3","4","5","6","7","8","9","0"]
for eachInvalid in invalid:
if eachInvalid in foo:
print("Not a real word!")
main()
else:
pass
main()
So far, it works but I am not proud of it. What would be the better way of doing this be? For example, a word does not contain things like ~@: so I would have to add those to that list. How would I avoid this but keep the code rather clean and readable?
Also, another error, if I run the code and type something in like hello, that is valid but if I type something invalid it takes me back to the start but when I type a valid word it still says invalid? For example:
Enter a word: hello
>>> ================================ RESTART ================================
>>>
Enter a word: 123
Not a real word!
Enter a word: hello
Not a real word!
How would I fix this error and what would be the best way of looking for invalid characters in an input?
edit: nevermind, regular expression is fine.