Feel free to critique my code and offer suggestions on how to clean it up and make it more presentable. I have been working on this for a few days, and learning as I go.
from __future__ import print_function
import random
import sys
#Global variables
dealerWins = 0
playerWins = 0
dealerCard = [1,2,3,4,5]
playersCard = [1,2,3,4,5]
def main():
intro()
def intro(): #Intro to game; asking for deposit of money into bank variable
print ('Welcome to the Back Alley Gambling Club!')
try:
bank = int(raw_input('How much money would you like to put in your account?: '))
if bank < 1:
print ("Sorry, input must be a positive integer, try again", '')
intro()
except ValueError:
print('Please input a valid positive integer')
intro()
bankString = str(bank)
print ('Do you want to put $'+bankString + ' into your account?')
checkBet = raw_input('Type \'y\' or \'n\': ')
if checkBet == 'y':
bet(bank)
else: intro()
def bet(bank): #Ask how much you want to wager
print ('How much would you like to wager?: ', end="")
try:
wager = int(raw_input())
if wager > bank:
print ('Your bet must be less than or equal to what is in your bank. Please try again.')
bet(bank)
else: checkBet(wager, bank)
except ValueError:
print('Please input a valid integer')
bet(bank)
def checkBet(wager,bank):
wagerString = str(wager)
print ('Awesome! You want to bet $'+ wagerString + ' is that right?: ')
check = raw_input('Type \'y\' or \'n\': ')
if check == 'y':
print ('Awesome, lets play!')
cardGame(wager, bank)
elif checkBet == 'n':
print ('Lets try again, shall we?')
checkBet(wager)
else:
print ('Please try again')
checkBet(wager)
def cardGame(wager,bank): #main logic of card game
dealerHand = (random.choice(dealerCard))
playersHand = (random.choice(playersCard))
print ('The Dealers card is', dealerHand)
print ('The Players card is', playersHand)
if dealerHand > playersHand:
print ('Dealer wins!')
global dealerWins
dealerWins+=1
if dealerWins<2:
print ('Dealer has',dealerWins, 'victory!')
else: print ('Dealer has', dealerWins,'victories!')
elif dealerHand < playersHand:
print ('Player wins!')
global playerWins
playerWins+=1
if playerWins<2:
print ('Player has', playerWins,'victory!')
else: print ('Player has', playerWins,'victories!')
else: print ('Push. Its a tie.')
if dealerWins < 3 and playerWins < 3:
print ('One more round.')
raw_input('Press enter to continue!')
cardGame(wager, bank)
elif dealerWins >= 3:
print ('Dealer wins the game!')
bank = bank-wager
print ('You now have ', bank, 'in your bank.')
playAgain(wager, bank)
else:
print ('Player wins the game!')
bank = bank + wager
print ('You now have ', bank, 'in your bank.')
playAgain(wager, bank)
def playAgain(wager, bank): #play again
global dealerWins
global playerWins
dealerWins =0
playerWins = 0
if bank <=0:
entry = raw_input('Would you like to play again? Type \'y\' or type \'n\' : ' )
if entry == 'y':
intro()
elif entry == 'n':
print ('Have a great day!')
sys.exit()
else: print ('Please type either \'y\' or \'n\'. Thank you!: ')
playAgain(wager, bank)
elif bank >0:
entry = raw_input('Would you like to play again? Type \'y\' or type \'n\' : ' )
if entry == 'y':
bet(bank)
elif entry == 'n':
print ('Have a great day!')
sys.exit()
else: print ('Please type either \'y\' or \'n\'. Thank you!: ')
playAgain(wager, bank)
main()