Python 同时循环相同的单词
我的代码有问题。它应该将用户输入的单词作为句子打印出来,除非用户输入单词“finish”或同一个单词两次。现在,如果同一个单词输入两次,它将不会结束循环,尽管我认为可以使用Python 同时循环相同的单词,python,Python,我的代码有问题。它应该将用户输入的单词作为句子打印出来,除非用户输入单词“finish”或同一个单词两次。现在,如果同一个单词输入两次,它将不会结束循环,尽管我认为可以使用if word==word:命令,但如何改进 句子=“” 尽管如此: word=输入(“请输入您的word:”) 如果word==“完成”: 打破 如果单词==单词: 打破 句子+=单词+“” 打印(句子) 这里的问题是,您要求您的代码仅在输入词正是输入词时停止循环,而不是在输入词已经包含该词时停止循环 这应该起作用: sen
if word==word:
命令,但如何改进
句子=“”
尽管如此:
word=输入(“请输入您的word:”)
如果word==“完成”:
打破
如果单词==单词:
打破
句子+=单词+“”
打印(句子)
这里的问题是,您要求您的代码仅在输入词正是输入词时停止循环,而不是在输入词已经包含该词时停止循环
这应该起作用:
sentence = " "
while True:
userInput = input("Please enter your word:")
if userInput == 'finish' or userInput in sentence:
break
sentence = sentence + " " + userInput
print(sentence)
这里的问题是,你要求你的代码只在输入的单词恰好是输入单词时停止循环,而不是在它已经包含该单词时要求它停止循环 这应该起作用:
sentence = " "
while True:
userInput = input("Please enter your word:")
if userInput == 'finish' or userInput in sentence:
break
sentence = sentence + " " + userInput
print(sentence)
你可以这样做
sentence = " "
word_copy = None
while True:
word = input("Please enter your word:")
if word == "finish":
break
if word_copy == word:
break
word_copy = word
sentence += word + " "
print(sentence)
在代码中,您正在检查(word==word),这将始终为真。
我希望你明白。你可以这样做
sentence = " "
word_copy = None
while True:
word = input("Please enter your word:")
if word == "finish":
break
if word_copy == word:
break
word_copy = word
sentence += word + " "
print(sentence)
在代码中,您正在检查(word==word),这将始终为真。
我希望你们明白这一点。现在word==word永远是正确的。您应该使用
prevWord
变量将最后一个单词存储在每个循环的末尾,然后比较prevWord
和当前单词,如prevWord==word
此时word==word将始终为真。您应该使用prevWord
变量将最后一个单词存储在每个循环的末尾,然后比较prevWord
和当前单词,如prevWord==word
,您需要将输入的单词与输入的最后一个单词进行比较,您可以通过获取句子中的最后一个单词来实现
sentence = ""
while True:
word = input("Please enter your word:")
if word == "finish":
break
if [word] == sentence.split(' ')[:-1]:
break
sentence += word + " "
print(sentence)
您需要将输入的单词与输入的最后一个单词进行比较,您可以通过获取句子中的最后一个单词来做到这一点
sentence = ""
while True:
word = input("Please enter your word:")
if word == "finish":
break
if [word] == sentence.split(' ')[:-1]:
break
sentence += word + " "
print(sentence)
试试看,但我似乎有点晚了 这里的解决方案也有独特的单词,如果写的是同一个单词,循环会中断:
sentence = []
while True:
word = input("Please enter your word:")
if word == 'finish' or word in sentence:
break
sentence.append(word)
print(' '.join(sentence))
试试看,但我似乎有点晚了 这里的解决方案也有独特的单词,如果写的是同一个单词,循环会中断:
sentence = []
while True:
word = input("Please enter your word:")
if word == 'finish' or word in sentence:
break
sentence.append(word)
print(' '.join(sentence))
您可以将之前的所有单词保存在列表中:
sentence = ""
words = []
while True:
word = input("Please enter your word: ")
if word == "finish":
break
# this checks if the word is already in the word list
elif word in words:
break
else:
# append the word to the word list
words.append(word)
sentence = sentence + word
这应该行得通:)
代码的问题是,
word
在程序每次运行while
-循环时都会更改,因为它只能有一个值。您可以将之前的所有单词保存在列表中:
sentence = ""
words = []
while True:
word = input("Please enter your word: ")
if word == "finish":
break
# this checks if the word is already in the word list
elif word in words:
break
else:
# append the word to the word list
words.append(word)
sentence = sentence + word
这应该行得通:)
代码的问题是,
word
在程序每次运行while
-循环时都会更改,因为它只能有一个值。另一个解决方案是使用列表,如果单词不等于finish或列表本身的最后一个元素,则只向所述列表添加输入
循环中断后,加入列表并打印它
sentence = [""]
while True:
word = str(input("Please enter your word:"))
if word != "finish" and word != sentence[-1]:
sentence.append(word)
else: break
sentence = ' '.join(sentence[1:])
print(sentence)
另一个解决方案是使用列表,仅当单词不等于finish或列表本身的最后一个元素时,才将输入添加到所述列表中 循环中断后,加入列表并打印它
sentence = [""]
while True:
word = str(input("Please enter your word:"))
if word != "finish" and word != sentence[-1]:
sentence.append(word)
else: break
sentence = ' '.join(sentence[1:])
print(sentence)
您需要一个临时变量来保存以前的值。您需要一个临时变量来保存以前的值。我收到以下错误:
AttributeError:'str'对象没有属性'str'
对不起,我的错误!我刚刚更正了代码,知道它是有效的:)@quamranaI获取此错误:AttributeError:'str'对象没有属性'str'
对不起,我的错!我刚刚更正了代码,知道它是有效的:)@quamrana