相同的输入2个不同的结果

Same input 2 different results

我对编程非常陌生,希望有人能帮助我。 我正在尝试制作一个游戏,其中 2 个玩家需要根据另一个玩家放置的单词的最后 2 个字母输入单词。 我让那部分工作,但我无法得到决定获胜者的部分。这是相同的 2 个 elif 语句,但它们应该打印出不同的结果。

例如。 P1:香蕉 P2:纳尼亚 P1:ian P2:animal 所以基本上当其中一名玩家未能完成匹配最后 2 个字母的任务时,他们输掉了游戏

 used_words=[]

 while True:
     player_one=raw_input("Player one \n")
     first= list(player_one)
     player_two=raw_input("Player two \n")
     second=list(player_two)

     if first[-2:] == second[:2] and first and second not in used_words:
         used_words.append(player_one)
         used_words.append(player_two)
         continue

     elif first[-2:] != second[:2]:
         print "Player one wins! \n"
         print "The word you had to match was: ", second
         break

     elif second[:2] != first[-2:]:
         print "Player two wins!"
         print "The word you had to match was: ", first
         break

    else:
         break

我认为问题出在你的条件 if first[-2:] == second[:2] and first and second not in used_words: 中,因为 and first 基本上是在测试 first 不是空字符串,所以将其更改为 if first[-2:] == second[:2] and first not in used_words and second not in used_words:。但是,为了实现您想要的,还应该进行其他更改:

player_one = raw_input("Player one \n")
used_words = [player_one]

while True:
    player_two = raw_input("Player two \n")        

    if used_words[-1][-2:] != player_two[:2] and player_two not in used_words:
         print "Player one wins! \n"
         print "The word you had to match was: ", player_one
         break

    used_words.append(player_two)
    player_one = raw_input("Player one \n")

    if used_words[-1][-2:] != player_one[:2] and player_one not in used_words:
         print "Player two wins! \n"
         print "The word you had to match was: ", player_two
         break