<分区>
我目前正在学习 Python,我有一个问题也找不到答案,目前我正在尝试获取用户提供的字符串变量并将其与另一个字符串的一部分进行比较。我想要这样的东西:
Program: The given sentence is "I like chemistry", enter a word in the sentence given.
User: like
Program: Your word is in the sentence.
我似乎只能使用 if
函数和 ==
来编写程序,但这似乎只能在我输入完整句子时识别出这两个字符串是相似的由程序给出。
从一些答案我已经改变了我的程序,但似乎有一个我找不到的错误。
sentence=("I like chemistry")
print("The given sentence is: ",sentence)
word=input("Give a word in the sentence: ").upper
while word not in sentence:
word=input("Give a valid word in the sentence: ")
if word in sentence:
print("valid")