# Basic 3 Round Card Game

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()


# PEP 8

Please follow the PEP 8 style guide. So, as some quick examples:

1. Use snake_case i.e. playAgain -> play_again
2. Add spaces between your operators: dealerWins =0 -> dealer_wins = 0
3. Add two spaces between each function.

There may be some others. Please read the link so you can find the issues, or run pylint on your code to correct all of these issues.

# Structure

You really should avoid using global variables unless you absolutely have to. In this case, you don't need them.

## Classes

Given that you have some global state at the moment, it might be a good idea to write a class describing your game.

class CardGame:

def __init__(self):
self.dealer_wins = 0
self.player_wins = 0

...


If you have issues understanding what classes are please read the documentation.

# Function Structure

The flow of your program is a bit weird. Your intro goes to bet which goes to checkBet, which then goes to the main cardGame function. I think having an intro is fine. The transition from intro to bet is fine as well. The issue I have is you call card game from checkBet. Your checkBet should determine if the bet is valid. Then if it is, let the function bet proceed into cardGame. Don't proceed from checkBet.

# Redundant string calls.

wagerString = str(wager) and bankString = str(bank) are unnecessary, just use the RHS.

### Repetition in input handling

There is so much repetition in input handling that the logic is almost obscured by it.

I would write a general function:

def ask_until(prompt, validation=lambda x:True, error_message=""):
x = input(prompt)
while not validation(x):
print(error_message)
x = input(prompt)
return x

def is_integer_and_within(lower, upper):
return lambda x: all(d in "1234567890" for d in x) and lower < x < upper


An use it like:

def bet(bank): #Ask how much you want to wager