我已经在Python 2.7中编写了Rock-Scissor-Paper游戏的基础,如下所示:

  1 #!/bin/py
  2 #Python 2.7.6
  3 from random import choice
  4
  5 moves = ["r", "p", "s"] # Possible moves
  6 winning_combos = {("r", "s"), ("s", "p"), ("p", "r")} # Winning   outcomes
  7
  8 def human():
  9         print
 10         print "R: Rock    P: Paper    S: Scissor"
 11         print
 12         human_move = raw_input("Enter your choice: ").lower()
 13         return human_move
 14 ()
 15
 16 def bot():
 17         bot_move = choice(moves)
 18         return bot_move
 19 ()
 20
 21 while True:
 22         human(), bot()
 23         if human() == bot():
 24                 print "Grr... a tie!"
 25         elif (human(), bot()) in winning_combos:
 26                 print "Woo-hoo! You win this round, human"
 27         else:
 28                 print "Bwahahaha! The almighty bot wins!"




我的问题是,如何才能在第25行中尝试比较human()和bot()函数的结果?当该程序按原样运行时,系统会提示我输入两次输入,然后再进行比较。

我可以用其他方式编写该程序以产生可接受的结果,因此我不希望重写该程序。我是专门(尽管可能没有有效地)询问如何按原样调整该程序(我对Python还是很陌生),以及了解我做错了什么。谢谢!

最佳答案

您的问题是您每次需要访问该值时都要调用humanbot。将结果存储在变量中,如下所示:

while True:
        human_move, bot_move = human(), bot()
        if human_move == bot_move:
                print "Grr... a tie!"
        elif (human_move, bot_move) in winning_combos:
                print "Woo-hoo! You win this round, human"
        else:
                print "Bwahahaha! The almighty bot wins!"

关于python - 正确比较功能输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37119962/

10-16 17:04