首页 > 解决方案 > 您可以打印布尔值的名称或参数的结果吗?

问题描述

我应该为一些机会游戏编写代码,而我正在开发一个抛硬币游戏。我几乎可以完全使用它,但我唯一仍然坚持的是打印实际的硬币翻转是什么。

当有人下注时,我希望结果显示

获胜者获胜者,硬币落在正面/反面!您现在还有 $n 可以赌博。

或者

哦——运气不好。硬币落在正面/反面,下次好运!您现在还有 $n 可以赌博。

它保持投注的连续记录。我已经尝试以两种方式打印结果,我将发布完整的当前代码和我尝试过的另一种方式的片段。我能得到的最好结果是硬币落在真/假或 1/2 上,我不知道如何得到我正在寻找的结果!

提前致谢。

完整代码:

import random
num = random.randint(1, 2)
money = 100
heads = num == 1
tails = num == 2
# heads = num % 2 == 0
# tails = num % 2 == 1
#Write your game of chance functions here

def coin_flip(call, bet):
  global money
  win = heads and call == heads or tails and call == tails
  lose = heads and call == tails or tails and call == heads
  if win:
    money += bet
    print("Winner winner, the coin landed on " + str(num) + "!")
    print("You now have $" + str(money) + " left to gamble.")
  else:
    money += -bet
    print("Ohh- tough luck. The coin landed on " + str(num) +", better luck next time!")
    print("You now have $" + str(money) + " left to gamble.")



#Call your game of chance functions here

coin_flip(heads, 30)

这产生 1/2 而不是正面/反面

有了这个变化:

 if win:
    money += bet
    print("Winner winner, the coin landed on " + str(call) + "!")
    print("You now have $" + str(money) + " left to gamble.")
  else:
    money += -bet
    print("Ohh- tough luck. The coin landed on " + str(call) +", better luck next time!")
    print("You now have $" + str(money) + " left to gamble.")

我知道硬币翻转是真/假。

我很确定我理解为什么这些没有给我想要的结果,但我不确定需要做什么才能得到我想要的。

标签: python

解决方案


你可以使用一个简单的字典

di = {1:"Heads",2:"Tails"}

然后,

print(di[num])

推荐阅读