基本上,我需要获取一个变量作为用户输入(ch),它当然只能包含大写字母和数字形式的字符串。我试图将用户的输入大写,即使他以小写格式提供它们工作正常,但现在我必须确保他没有使用任何符号(如你在 forbidenCh 中看到的),但我的想法没有奏效在这里帮助我你可以使用任何你想要的方法,只要它完成了程序的目的,谢谢
这是我的尝试:
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
ch = str(input("only give letters and numbers: "))
ch = ch.upper()
forbidden_chars = "!#$%&'()*+,-./:;<=>?@[\]^_`{|}~"
# enter for-loop
for forbidden_chars in ch:
# checking if forbidden_chars is in user input, if true ask for
# new input
if forbidden_chars in ch:
ch = str(input("u didn't put capitalized letters and numbers!: "))
ch = ch.upper()
else:
break
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句