如何编写一个 while 循环,要求用户输入,并在连续使用同一值两次时中断?
这是我当前的尝试,尝试使用
len(set(word))
来获取前一个单词:
story = ""
while True:
word = input("Give me a word: ")
if word == "end" or word == len(set(word)):
break
story += word + " "
print(story)
连续两次,我只会记住前一个单词
story = ""
prev = ""
while True:
word = input("Give me a word: ")
if word == "end" or word == prev:
break
prev = word
story += word + " "
print(story)
我会这样做:
story = []
prev = ""
while True:
word = input("Give me a word: ")
if word == "end" or word == prev:
break
prev = word
story.append(word+" ")
print(''.join(story))
'''Program to take single word as input from users,
combine input word as a string and end the program
if any of the word in the string is repeated'''
import re
storyString = ""
def takeInput(storyString):
global inputWord
inputWord = input("Give me a word: ")
creatWordFromString(storyString)
def creatWordFromString(storyString):
storyList = storyString.split()
if inputWord in storyList:
exit()
else:
storyList.append(inputWord)
storyString =' '.join(storyList)
print(storyString)
takeInput(storyString)
takeInput(storyString)