您可以打印布尔名称还是参数结果?

时间:2019-06-21 11:16:47

标签: python

我应该为一些偶然的游戏编写代码,并且正在从事抛硬币游戏。我几乎可以完全使用它了,但是我唯一仍然遇到的问题是打印实际的硬币翻转情况。

当某人下注时,我希望结果要么说

  

优胜者,硬币落在头/尾上!   现在您剩下$ 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.")

我发现硬币翻转是对/错。

我几乎可以肯定为什么我没有得到想要的结果,但是我不确定要做什么才可以得到我想要的结果。

2 个答案:

答案 0 :(得分:2)

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

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

然后

print(di[num])

答案 1 :(得分:0)

尝试:

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

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



#Call your game of chance functions here

coin_flip(heads, 30)