51学通信论坛2017新版

标题: 21根棍子游戏 [打印本页]

作者: admin    时间: 2018-7-31 15:42
标题: 21根棍子游戏
这是一个非常简单的游戏。这里有21根棍子,首先用户选1到4根棍子,然后电脑选1到4根棍子。谁选到最后一根棍子谁就输。你知道哪种情况用户会赢吗?

(其实答案是:用户永远都赢不了。谁先选谁必输,看完代码就知道了)。
代码:
  1. sticks = 21

  2. print("There are 21 sticks, you can take 1-4 number of sticks at a time.")
  3. print("Whoever will take the last stick will loose")

  4. while True:
  5.     print("Sticks left: " , sticks)
  6.     sticks_taken = int(input("Take sticks(1-4):"))
  7.     if sticks == 1:
  8.         print("You took the last stick, you loose")
  9.         break
  10.     if sticks_taken >= 5 or sticks_taken <= 0:
  11.         print("Wrong choice")
  12.         continue
  13.     print("Computer took: " , (5 - sticks_taken) , "\n")
  14.     sticks -= 5
复制代码





欢迎光临 51学通信论坛2017新版 (http://bbs.51xuetongxin.com/) Powered by Discuz! X3