如何在Python中隐藏值? (显示为*)

时间:2018-04-26 19:24:07

标签: python python-3.x python-2.7

我在python中制作一个简单的游戏,我想在开头显示所有数字,如下所示:

* * *
* * *
* * *

然后在您选择一个位置(例如2,3)后,它将显示该位置的数字。

所以在我的班上我想初始化这些要点:

def __init__(self, val):
    self.val = val
    # I want to set the created number to be displayed as *

1 个答案:

答案 0 :(得分:0)

这应该是足够的代码来帮助您入门。您只需要一个标志来确定是否应该隐藏号码:

grid = [
  [{"number": 1, "hidden": True}, {"number": 5, "hidden": False}, {"number": 8, "hidden": True}],
  [{"number": 3, "hidden": True}, {"number": 2, "hidden": True}, {"number": 4, "hidden": False}],
  [{"number": 9, "hidden": True}, {"number": 7, "hidden": False}, {"number": 3, "hidden": False}]
]

for row in grid:
  for column in row:
      print("*" if column["hidden"] else column["number"], "", end="")
  print()

打印:

* 5 * 
* * 4 
* 7 3