Hello again! Today’s game is called HEADS or TAILS, but with a twist… You have money and bets!
In other words, you input how much money you have and how much you’re willing bet.
You either bet HEADS or TAILS (duh…), if you win, you’ll multiply the money you bet by two and add to your existing money; if you fail, you lose the money you bet.
Simple, right?
I chose to polish up my program (even though it has some bugs) to add more syntactical clarity , but you can simply code a quick, functional program.
As always, feel free to post your code in other languages!
Happy Coding!
from random import randint
def game():
mon = int(raw_input('\n[*] How much money do you have? > '))
while mon > 0:
r = randint(0,1) #generates either 0 or 1. 1 = 'heads'; 0 = 'tails'
bet = int(raw_input('[*] Place your bet > '))
if bet > mon:
print "\n[!] You don't have that much money!"
elif mon == 0:
print '\n[!] You are out of money!\n'
else:
hd = raw_input('\n[*] All right, the million dollar question, HEADS or TAILS? > ')
if (hd == 'heads' and r == 1) or (hd == 'tails' and r == 0):
print '\n[!] You won!\n'
mon += bet * 2
print '> You now have %s!\n' % (mon)
elif (hd == 'heads' and r == 0) or (hd == 'tails' and r == 1):
print '\n[!] You lost...\n'
mon -= bet
print '> You now have %s!\n' % (mon)
else:
print '\n[!] Please choose HEADS or TAILS\n'
def main():
print '#----------------#'
print '| HEADS OR TAILS |'
print '| by n3xUs_ |'
print '#----------------#'
print '\n[*] This game is simple. You either choose HEADS or TAILS.\n'
print '[*] If you win, you multiply the money you bet, if you lose, you lose the money.\n'
print '[*] Simply input how much money you have and how much you want to bet.'
raw_input('\n[*] Press ENTER to begin...')
game()
main()
Also, great code @Defalt, might change my own based on yours.
You can really see the difference between a beginner/intermediate (me) and an advanced coder like yourself!
#! /usr/bin/ruby
$balance = 100
def flipCoin(bet, predict)
if ((bet == 0) or (bet > $balance))
puts '[!] Invalid Bet!'
return
end
return if (predict != 'heads' and predict != 'tails')
result = Random.new().rand(2)
if ((result == 0 and predict == 'heads') or (result == 1 and predict == 'tails'))
puts '[*] You won!'
$balance += bet*2
else
puts '[*] You lost!'
$balance -= bet
end
end
def getBet()
begin
print "[*] Enter Bet: "
bet = gets.chomp.to_i
print "[*] Heads or Tails: "
predict = gets.chomp.downcase
return [bet, predict]
rescue
end
end
while true
print "Menu:\n1. Play\n2. Check Balance\n3. Exit\n"
print "> "
choice = gets.chomp
begin
case choice
when "1"
userBet = getBet()
flipCoin(userBet[0], userBet[1])
when "2"
puts "[*] Balance: #{$balance}"
when "3"
exit
end
rescue
end
end