2

我可以请你帮忙吗?

我必须在 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!")

但有些不对劲。因为当我启动程序时,我会写前两个字母。它说字典里没有这个词。

请帮我!

还有一件事:游戏必须在第一个错误字符后立即停止。你能告诉我怎么做这个吗?

4

4 回答 4

1

那是因为你的条件不对!您不应该检查 是否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!"
于 2012-10-21T09:02:22.787 回答
1

你的规则不一致。考虑这个游戏:

 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!")
于 2012-10-21T09:10:30.463 回答
1

你必须改变你的 while 条件。你这样做的方式是检查word内部是否是dictionary一个单词,而不是你想要的单词的一部分。您可以通过以下方式进行操作:

while any(x.startswith(word) for x in dictionary): 

这样,您可以创建一个布尔值列表,其中每个元素都是True如果字典中相同位置的单词以您的word. 如果字典的任何单词以word条件开头,则为真,玩家应输入另一个字符。

于 2012-10-21T09:11:40.787 回答
1

由于这是一个类,我假设它必须有点简单。这是一个如何使用函数工作的超级基本示例(不确定这些是否已包含在您的课程中,如果没有,请忽略):

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
于 2012-10-21T09:14:45.737 回答