不区分大小写的用户输入字符串 [英] Case insensitive user input strings

查看:27
本文介绍了不区分大小写的用户输入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个函数用于使用户输入字符串不区分大小写?

Which function to use for making user input strings case insensitive?

correctAnswer = "London"

userGuess = input("What is the capital of  Great Britain?: ")

if userGuess == "London":
    print("Correct!")
else:
    print("Wrong")

我在字符串后尝试了以下函数:

I have tried the following functions after the string:

.lower()
.capitalize()
.casefold()

输出仍然是错误的",尽管输入时答案应该是正确的:
- 伦敦
- 伦敦
- 伦敦

The output is still 'wrong' although the answer should be correct when input is:
- london
- lOndoN
- loNDON

等等..

推荐答案

在您的字符串比较中,正确答案本身的第一个字母大写.

In your string comparison, the correct answer itself had the first letter capitalized.

correctAnswer = "london"

userGuess = input("What is the capital of  Great Britain?: ").lower()

if userGuess == correctAnswer:
    print("Correct!")
else:
    print("Wrong")

这篇关于不区分大小写的用户输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆