I would like to improve my code. I am not a Python expert, so I am glad for any help. I would like to remove redundancies, unnecessary code, and change things that might shorten it.
#!/usr/bin/env python3
import skilstak.colors as c
import random as r
def computerPlay():
while cscore < 17:
chand.append(deck[position])
del deck[position]
cscore = sum(chand)
else:
if 11 in chand:
chand.remove(11)
chand.append(1)
cscore = sum(chand)
if cscore > 21:
winOrLose()
else:
computerPlay()
def winOrLose():
global cwins
global wins
global credits
cBet = int(input("How many credits would you like to bet? > "))
cscore = sum(chand)
score = sum(hand)
if cscore > 21:
cbust = "y"
if score > 21:
bust = "y"
else:
bust = "n"
else:
cbust = "n"
if score > 21:
bust = "y"
else:
bust = "n"
print(c.cl + "The computer's hand was" + str(chand))
print("Your hand was" + str(hand))
if cbust == "y":
if bust == "y":
print(c.yellow + "It's a tie!" + c.base01)
print("P",wins,": C",cwins)
else:
print(c.green + "You won!" + c.b01)
wins += 1
print("P",wins,": C",cwins)
credits += cBet
print("You now have",credits)
elif bust == "y":
if cbust == "y":
print(c.yellow + "It's a tie!" + c.b01)
print("P",wins,": C",cwins)
else:
print(c.red + "You lost."+ c.b01)
cwins += 1
print("P",wins,": C",cwins)
credits -= cBet
print("You now have",credits)
else:
if cscore > score:
print(c.red + "You lost."+ c.b01)
cwins += 1
print("P",wins,": C",cwins)
credits -= cBet
print("You now have",credits)
elif cscore < score:
print(c.green + "You won!" + c.b01)
wins += 1
print("P",wins,": C",cwins)
credits += cBet
print("You now have",credits)
else:
print(c.yellow + "It's a tie!" + c.b01)
print("P",wins,": C",cwins)
playAgain()
def playAgain():
again = input("Would you like to play again?(y/n) > ")
if again == "y":
print(c.cl)
del hand[:]
del chand[:]
deck = [2,3,4,5,6,7,8,9,10,10,10,10,11] * 4
position = 0 #Which card to pull out of deck
shuffleAndStart()
newcard()
else:
exit()
def newcard():
score = sum(hand)
while score <= 21:
userCard = input(c.b01 + "Would you like a new card?(y/n) > ")
if userCard == "y":
hand.append(deck[position])
score = sum(hand)
del deck[position]
print(c.cl + "Your hand contains " + str(hand) + " for a total of",score,"points.")
else:
computerPlay()
break
else:
if 11 in hand:
hand.remove(11)
print(c.cl + "Your ace valued 11 was changed into a 1")
hand.append(1)
score = sum(hand)
print("You now have"+str(hand)+"and",score,"Points")
if score > 21:
print("Oh no! You busted with:",score,"points and a hand of" + str(hand))
discard.append(hand)
computerPlay()
else:
newcard()
else:
print("You busted with:",score,"points and a hand of" + str(hand))
discard.append(hand)
computerPlay()
def shuffleAndStart():
print(c.cl)
r.shuffle(deck)
r.shuffle(deck)#shuffles the deck twice
hand.append(deck[position]) #adds new card to hand
score = sum(hand) # gets score
del deck[position] #removes card from deck
print("Your hand contains a:",score)
if __name__ == '__main__':
print(c.cl + c.b01 + "Welcome to BlackJack v.1.0! Created by Peter")
print("You have 500 credits.")
deck = [2,3,4,5,6,7,8,9,10,10,10,10,11] * 4
hand = []
credits = 500
chand = []
cwins = 0
wins = 0
discard = []
position = 0 #Which card to pull out of deck
shuffleAndStart()
newcard()