为什么我不能在这段代码上使用 break ,我可以用什么代替?Python

我正在做的任务是开发一个程序来识别句子中的单个单词,将它们存储在一个列表中,并用该单词在列表中的位置替换原始句子中的每个单词。

sentencelist=[] #variable list for the sentences
word=[] #variable list for the words
positions=[]
words= open("words.txt","w")
position= open("position.txt","w")

question=input("Do you want to enter a sentence? Answers are Y or N.").upper()
if question=="Y":
    sentence=input("Please enter a sentance").upper() #sets to uppercase so it's easier to read
    sentencetext=sentence.isalpha or sentence.isspace()
    while sentencetext==False: #if letters have not been entered
        print("Only letters are allowed") #error message
        sentence=input("Please enter a sentence").upper() #asks the question again
        sentencetext=sentence.isalpha #checks if letters have been entered this time

    word = sentence.split(' ')
    for (i, check) in enumerate(word): #orders the words
        print(sentence)

        word = input("What word are you looking for?").upper() #asks what word they want
        if (check == word):
            positionofword=print("your word is in this position:", i+1)
            positionofword=str(positionofword)
        else:
            print("this didn't work") #print error message

elif question=="N":
    print("The program will now close")
else:
print("you did not enter one of the prescribed letters")

words.write(word + " ")
position.write(positionofword + " ")

我的问题是我陷入了以下循环:

   word = input("What word are you looking for?").upper() #asks what word they want
    if (check == word):
        positionofword=print("your word is in this position:", i+1)
        positionofword=str(positionofword)
    else:
        print("this didn't work") #print error message

因此,这意味着我无法将这些词放入文件中。我曾尝试使用 break,但这对我不起作用,因为我无法将这些词放入文件中。

我是这个网站的新手,但我已经跟踪了一段时间。希望这是正确的,如果我措辞错误,我愿意听取批评。

布伦丹·多尼根

您在 for 循环中的逻辑不正确 - 而不是询问用户想要查找的单词,而是询问句子中的每个单词,并且只有当他们输入他们想要的单词时才匹配当前正在检查的单词. 您还为句子中的每个单词打印了一次句子。像这样重构它:

print(sentence)
sentence_words = sentence.split(' ')
word = input("What word are you looking for?").upper() #asks what word they want
for (i, check) in enumerate(sentence_words): #orders the words
    if (check == word):
        print("your word is in this position:", i+1)
        positionofword=i+1
        break
else:
    print("This didn't work")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我可以用什么代替@?

为什么我不能在这段代码中使用jQuery在HTML中添加一行?

'event' 已弃用,我可以用什么代替?

我可以用什么代替strstream :: freeze?

我可以用什么代替gnome-open?

我可以用什么代替滑动抽屉

为什么我不能在匿名方法中的while循环中使用break?

为什么我不能在 C# 三元表达式中使用 break?

如何获得命令提示符输出?为什么我不能在这段代码中得到它

使用什么代替BREAK(PYTHON)

为什么我必须使用可选的“?” 在这段代码中?

为什么我不能在这些代码行上应用pandas.cut()?

我可以用$('form')代替$吗?

如果我只想使用基于ubuntu的解决方案,我可以用什么代替ESXi?

为什么我不能在C ++中的三元条件语句中使用“ break”语句?

为什么这段代码可以在 Linux 上运行,而不能在 Windows 上运行?

为什么在这段代码中我不能让 indexOf() 等于 0 以外的任何值?

为什么我不能在我的代码中使用“ balance ”?

对于 next.js,我可以用什么代替 redux

我可以用什么代替 Debug.Ray 来统一绘制很多线

我很好奇为什么我们在这段代码中使用cache [n]

为什么我在这段代码中出现细分错误

为什么在这里需要'break;'?

Python PIL - 为什么我不能在这里使用 ImageOps.colorize。?

为什么我在这段代码中使用了太多的磁盘空间?

为什么我不能在这个特性中使用我的实例?

为什么我不能在Java枚举上使用foreach?

为什么我不能在 DataFrame 上使用 sort 或 orderby?

为什么我不能在Realm物业上使用private