相同输入2个不同的结果

时间:2017-09-20 19:32:02

标签: python if-statement words

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

实施例。 P1:香蕉P2:纳尼亚P1:伊恩P2:动物 因此,基本上当其中一个玩家未能完成匹配最后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

2 个答案:

答案 0 :(得分:-1)

您应该实际更改比较方案

 used_words=[]

 while True:
     word=raw_input("Enter sth \n")

     if(len(used_words) == 0):
         used_words.append(word)
         continue

     if(used_words[-1][-2:] == word[:2] and word not in used_words):
         used_words.append(word)

     elif(word in used_words):
         print "You lose! \n"
         print word, " is previously used"
         break

     elif(used_words[-1][-2:] != word[:2]):
         print "You lose! \n"
         print "The first two characters you had to start with was: ", used_words[-1][-2:]
         break
     else:
         break

答案 1 :(得分:-2)

我认为问题出在您的条件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