python 3.x - Checking if a variable contains spaces and letters -
i trying make program asks name reject input if doesn't contain letters/spaces. however, seems reject spaces numbers , symbols.
print("welcome basic arthmetics quiz.") print("what name?") name=input() if not name.isalpha()or name not in(str(" ")): print('please enter letters name!') while not name.isalpha()or name in(str(" ")): v=1 print('please enter name again.') name=input() if name.isalpha()or name not in(str(" ")): v=0 else: v=1
where have gone wrong?
this looks awful lot homework.
your test name not in(str(" "))
, should written name not in " "
, tests whether name 1 of {"", " "}
.
it easiest test name 1 char @ time, per-char condition like
char.isalpha() or char == ' '
combine all
, generator expression test chars of name
.
the actual implementation, proper code flow (you don't use v
, perform test thrice, , call input()
twice, of unacceptable) left exercise.
Comments
Post a Comment