Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Nancy Melucci
Courses Plus Student 36,159 PointsPython while loop won't exit at sentinel and processes letter as integer.
I am trying to write a simple blackjack program that takes bets and exits when the user enters "x". But when x is entered, it appears to process it as an integer and continue the loop.
I've posted the complete code below. If anyone could show me what I am missing, I'd be grateful.
# blackjackpluspluw.py NJM 2/23/2018
import random
def startingmoney(mybankroll):
mybankroll = input('Starting Money: ')
mybankroll = int(mybankroll)
while (mybankroll < 0 or mybankroll > 10000):
print('Invalid amount. Must be between 0 and 10,000')
mybankroll = input('Starting Money: ')
mybankroll = int(mybankroll)
return mybankroll
def greeting():
print("BLACKJACK!\nBlackjack payout is 3:2")
print("Enter 'x' for bet to exit")
print("")
return 0
#function where problem happens. Tried putting the while loop here previously
def makebet(bet, money):
bet_amount = input("Bet Amount: ")
try:
bet = int(bet_amount)
except ValueError:
print("Bye")
finally:
if bet > money:
print('You don\'t have enough to make that bet')
makebet(bet, money)
elif bet < 5:
print('Minimum bet is 5')
elif bet > 1000:
print('Maximum bet is 1000')
else:
money = money - bet
outcome = draw()
winnings = blackjack_table(bet, outcome)
money = money + winnings
print("Money:", money)
print("")
makebet(bet, money)
return 0 # function to calculate winnings
def blackjack_table(bet, play):
if play.lower() == "b":
bet = bet * 2.5
if play.lower() == "w":
bet = bet * 2
if play.lower() == "l":
bet = 0
if play.lower() == "p":
bet = bet
return bet
def draw():
hands = ['b', 'w', 'p', 'l']
rand_item = hands[random.randrange(len(hands))]
if rand_item == 'b':
print("You win.")
elif rand_item == 'w':
print("You win.")
elif rand_item == "p":
print("You push.")
else:
print("You lose.")
return rand_item
# main
def main():
# meet and greet with exit strategy
greeting()
mybankroll = 0
money = startingmoney(mybankroll)
bet_amount = "0"
#also tried it here
while bet_amount != "x":
makebet(bet_amount, money)
print("Bye")
# end program
print(" ")
# run program
main()
3 Answers
David Deberry
5,447 PointsI tried to fix it completely but I couldn't. I changed finally to else because if you leave it as finally it will run even if the try throws an error and that will cause it to throw an error lol. The else only runs if try doesn't throw an error i believe.
Next I changed the return value for makebet to bet_amount so that it could be checked by the loop before entering it again.
That was all. It works but for some reason you have to enter x about 2 or 3 times before it exits and say bye.
hopefully you will have better luck at fixing that.
# blackjackpluspluw.py NJM 2/23/2018
import random
def startingmoney(mybankroll):
mybankroll = input('Starting Money: ')
mybankroll = int(mybankroll)
while (mybankroll < 0 or mybankroll > 10000):
print('Invalid amount. Must be between 0 and 10,000')
mybankroll = input('Starting Money: ')
mybankroll = int(mybankroll)
return mybankroll
def greeting():
print("BLACKJACK!\nBlackjack payout is 3:2")
print("Enter 'x' for bet to exit")
print("")
return 0
#function where problem happens. Tried putting the while loop here previously
def makebet(bet, money):
bet_amount = input("Bet Amount: ")
try:
bet = int(bet_amount)
except ValueError:
print("Bye")
else:
if bet > money:
print('You don\'t have enough to make that bet')
makebet(bet, money)
elif bet < 5:
print('Minimum bet is 5')
elif bet > 1000:
print('Maximum bet is 1000')
else:
money = money - bet
outcome = draw()
winnings = blackjack_table(bet, outcome)
money = money + winnings
print("Money:", money)
print("")
makebet(bet, money)
return bet_amount.lower() # function to calculate winnings
def blackjack_table(bet, play):
if play.lower() == "b":
bet = bet * 2.5
if play.lower() == "w":
bet = bet * 2
if play.lower() == "l":
bet = 0
if play.lower() == "p":
bet = bet
return bet
def draw():
hands = ['b', 'w', 'p', 'l']
rand_item = hands[random.randrange(len(hands))]
if rand_item == 'b':
print("You win.")
elif rand_item == 'w':
print("You win.")
elif rand_item == "p":
print("You push.")
else:
print("You lose.")
return rand_item
# main
def main():
# meet and greet with exit strategy
greeting()
mybankroll = 0
money = startingmoney(mybankroll)
bet_amount = None
#also tried it here
while bet_amount != "x":
bet_amount = makebet(bet_amount, money)
print("Bye")
# end program
print(" ")
# run program
main()
Nancy Melucci
Courses Plus Student 36,159 PointsThanks for trying. I am doing a few more debugging exercises and will take another whack at it shortly.
Nancy Melucci
Courses Plus Student 36,159 PointsI was able to import sys and add an exit statement after the ValueError. Worked well enough to submit it for grading. Thanks very much for tweaking it a little so I could make that work.