基本上,我需要一个变量作为用户输入(ch),它只能包含大写字母和数字,当然是字符串形式。我试图将用户的输入大写,即使他以小写格式输入,效果很好,但现在我必须确保他没有使用任何符号(如你在禁书中看到的),但我的想法不起作用,请帮我这里你可以使用任何你想要的方法,只要它达到程序和thnx的目的
这是我的尝试:
ch=str(input("only give letters and numbers"))
ch= ch.upper()
forbidenCh="!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
for i in forbidenCh:
for j in ch:
if i == j:
ch=str(input("u didn't put captilized letters and numbers !!"))
ch= ch.upper()
else:
break
可能只检查允许的字符更容易:
你可以做:
对于此类问题,使用ascii值更容易、更有效
check this out
代码:
阅读更多here
相关问题 更多 >
编程相关推荐