[英]How to loop characters in string and check if it is in dictionary python
請允許我幫忙。
我必須用python為homewrok編寫一個程序,它要求兩個競爭對手交替輸入一個字母。 當詞典中沒有這樣的單詞時,程序結束(我導入了從老師那里得到的文本格式的詞典)。
它看起來應該像這樣:
競爭對手1,字母a: m
競爭對手2,字母b: o
競爭對手1,字母a: u
競爭對手2,字母b: s
競爭對手1,字母a: e
競爭對手2,字母b: i
字典中沒有這樣的單詞mousei!
這是我的開始方式:
dictionary=open("dictionary.txt", encoding="latin2").read().lower().split()
a=input("Competitor 1, letter a:")
b=input("Competitor 2, letter b:")
word=a+b
while word in dictonary:
a=input("Competitor 1, letter a:")
word=word+a
b=input("Competitor 2, letter b:")
word=word+b
print("There is no such word" ,word, "in dictionary!")
但是出了點問題。 因為當我啟動程序時,我寫了前兩個字母。 它說字典中沒有這樣的詞。
請幫我!
還有一件事:游戲必須在第一個錯誤角色之后立即停止。 你能告訴我如何做到這一點。
那是因為你的情況不對! 您不應該檢查word
是否在字典中,但是字典中是否有以單詞開頭的word
!
這是一種可能的解決方案(可能不是最漂亮的一種):
word_ok = True
dictionary=open("dictionary.txt", encoding="latin2").read().lower().split()
a=raw_input("Competitor 1, letter a:")
b=raw_input("Competitor 2, letter b:")
word=a+b
while word_ok:
word_ok = False
for dict_word in dictionary:
if dict_word.startswith(word):
word_ok = True
break
if word_ok:
a=raw_input("Competitor 1, letter a:")
word=word+a
b=raw_input("Competitor 2, letter b:")
word=word+b
else:
break
print("There is no such word" ,word, "in dictionary!")
編輯:
好的,這是其他答案的一種匯編。 請注意, encoding
不是內置open()
函數的有效關鍵字參數。 如果要指定編碼,請使用codecs.open()
。 還要注意,在每個競爭對手輸入之后,在while
循環中兩次進行了單詞檢查。
import codecs
dictionary = codecs.open('dictionary.txt','r',encoding='latin2').read().lower().split()
# Word we will build
word = ''
while True:
letter = raw_input("C1: ")
word = word + letter
if not any(known_word.startswith(word) for known_word in dictionary):
break
letter = raw_input("C2: ")
word = word + letter
if not any(known_word.startswith(word) for known_word in dictionary):
break
print "There is no such word", word, "in dictionary!"
您的規則不一致。 考慮一下這個游戲:
s # Not a word
su # Not a word
sun # Word
sund # Not a word
sunda # Not a word
sunday # Word
游戲什么時候結束?
字典中沒有這樣的單詞時程序結束
您的規則說,它應該從第一步開始就結束,因為結果一言不發。 更好的規則是:
當輸入中沒有字典中的單詞時,程序結束
“交替問兩個競爭對手”的實施也是錯誤的。 您的程序要求兩個競爭者都寫一封信,然后檢查單詞。 您想檢查玩家之間的單詞。
dictionary = open("dictionary.txt", encoding="latin2").read().lower().split()
word = ""
c1turn = True
while any(knownword.startswith(word) for knownword in dictonary):
if c1turn:
letter = input("Competitor 1:")
else:
letter = input("Competitor 2:")
word = word + letter
c1turn = not c1turn
print("There is no such word", word, "in the dictionary!")
您必須更改自己的時間條件。 操作方法是檢查word
是否在dictionary
作為單個單詞出現,而不是您希望的單詞不屬於單詞的一部分。 您可以通過以下方式進行操作:
while any(x.startswith(word) for x in dictionary):
這樣,如果字典中相同位置的word
以您的word
開頭,則可以創建一個布爾值列表,其中每個元素均為True
。 如果有任何的字典的單詞開頭word
的條件將是真實的和玩家需要輸入其他字符。
由於這是一堂課,所以我假設它必須有點簡單。 這是一個使用函數的超級基本示例(不確定類中是否覆蓋了這些,因此請忽略):
def CheckWord(word, dictionary):
# Check if any words in your dictionary start with the current word
for term in dictionary:
if term.startswith(word):
return True
return False
# Basic dictionary
dictionary = ['mouse', 'louse']
# Word we will build
word = ''
# Keep the loop going until we get a False from our function, at which point it stops
while True:
a = raw_input("C1: ")
word += a
b = raw_input("C2: ")
word += b
# Check the current word with our function
match = CheckWord(word, dictionary)
# If the function returns False, quit
if not match:
print ("There is no such word", word, "in dictionary!")
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.