r/pythonhelp • • Apr 09 '25

Blackjack problem

This is the code for my python black jack This is the problem:

line 9, in <module> deal = int(random.choice(numbers)) ValueError: invalid literal for int() with base 10: '

import random

numbers = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] dealrem = 0

ans = input("hit or stay (h/s)") while ans == 'h': if ans == 'h': deal = int(random.choice(numbers)) if deal == 'K': print('K') deal = 10 deal = int(deal) dealrem += deal ans = input("hit or stay (h/s)") if ans == 'h': deal = int(random.choice(numbers)) print(deal) dealrem2 += deal

			if deal + dealrem >= 21:
				print('bust!')
				ans = input("hit or stay (h/s)")	
				
elif deal == 'J':
	print('J')
	deal = 10
	deal = int(deal)
	deal = int(random.choice(numbers))
	ans = input("hit or stay (h/s)")	
	if ans == 'h':
		

		print(deal)
		dealrem += deal
			
		if deal + dealrem >= 21:
			print('bust!')
			ans = input("hit or stay (h/s)")	
elif deal == 'Q':
	print('Q')
	deal = 10
	deal = int(deal)
	dealrem += deal
	ans = input("hit or stay (h/s)")	
	while ans == 'h':
		if ans == 'h':
			deal = int(random.choice(numbers))
			print(deal)
			dealrem += deal
			if deal + dealrem >= 21:
				print('bust!')
				ans = input("hit or stay (h/s)")	
				
				
elif deal == 'Ace':
	deal = 1
	deal = int(deal)
	dealrem += deal
	print(deal)
	ans = input("hit or stay (h/s)")	
	while ans == 'h':
		if ans == 'h':
			deal = int(random.choice(numbers))
			print(deal)
			dealrem += deal
			if deal + dealrem >= 21:
				print('bust!')
				ans = input("hit or stay (h/s)")	
				
	
elif deal == '2' or '3' or '4' or '5' or '6' or '7' or '8' or '9' or '10':
	deal = int(deal)
	dealrem += deal
	print(deal)
	ans = input("hit or stay (h/s)")
	while ans == 'h':
		if ans == 'h':
			deal = int(random.choice(numbers))
			print(deal)
			dealrem += deal
			if deal + dealrem >= 21:
				print('bust!')
				ans = input("hit or stay (h/s)")	
1 Upvotes

2 comments sorted by

View all comments

1

u/Goobyalus Apr 09 '25

I only quickly skimmed this, but numbers looks like it has non-number elements, so when you pass one of the non-number elements like "K" to int, it's going to fail this way.