vec*_*ife 1 python user-input case-insensitive
使用哪个函数使用户输入字符串不区分大小写?
correctAnswer = "London"
userGuess = input("What is the capital of Great Britain?: ")
if userGuess == "London":
print("Correct!")
else:
print("Wrong")
Run Code Online (Sandbox Code Playgroud)
我在字符串后尝试了以下功能:
.lower()
.capitalize()
.casefold()
Run Code Online (Sandbox Code Playgroud)
尽管输入在以下情况时答案应该是正确的,但输出仍然是“错误的”:
-伦敦
-lOndoN
-loNDON
等等..
在字符串比较中,正确答案本身的首字母大写。
correctAnswer = "london"
userGuess = input("What is the capital of Great Britain?: ").lower()
if userGuess == correctAnswer:
print("Correct!")
else:
print("Wrong")
Run Code Online (Sandbox Code Playgroud)