我目前正在尝试计算用户输入的句子中的单词数量,并计算单词“ Python”在该句子中的使用次数,并在最后显示两个结果。 我试图将句子强制转换为小写字母,但目前不确定如何将其集成到代码中。到目前为止,它计算的是输入“ Python”的次数,而不是“ python”的次数,我需要同时注册两者。
const rotation = this.state.iconRotated ? 90 : 0
<TouchableOpacity onPress={this.toggleExpanded}>
<View style={{ alignItems: "center" }}>
<Icon
iconStyle={
{
paddingTop:"13%",
paddingRight: 50,
transform: `${rotate(rotation)}`
}
}
name="play-circle"
color="#637182"
type="light"
size={30}
/>
</View>
</TouchableOpacity>
答案 0 :(得分:2)
首先将整个输入句子转换为小写
sentence = input(str("Please enter a sentence with your thoughts on Python:"))
howManyPython = sentence.lower().count ("python")
listOfWords = len(sentence.split())
print("You said Python " + str(howManyPython) + " times!")
print("There was " + str(listOfWords), "words in your sentence")
# output
Please enter a sentence with your thoughts on Python:python 123 Python Python
You said Python 3 times!
There was 4 words in your sentence
答案 1 :(得分:0)
您可以将整个输入字符串转换为小写字母,以计算单词“ Python”:
sentence = 'Python python python3000 py'
listOfWords = len(sentence.split())
howManyPython = sentence.lower().count("python") # convert to lowercase
print("You said Python " + str(howManyPython) + " times!")
print("There was " + str(listOfWords) + " words in your sentence")
返回:
You said Python 3 times!
There was 4 words in your sentence