Python Forum
Why is the item not in list when it is
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is the item not in list when it is
#1
#blackjack

import random
deck = ["2+diamonds","3+diamonds","4+diamonds","5+diamonds","6+diamonds","7+diamonds","8+diamonds","9+diamonds","10+diamonds","j+diamonds","q+diamonds","k+diamonds","a+diamonds","2+clubs","3+clubs","4+clubs","5+clubs","6+clubs","7+clubs","8+clubs","9+clubs","10+clubs","j+clubs","q+clubs","k+clubs","a+clubs","2+hearts","3+hearts","4+hearts","5+hearts","6+hearts","7+hearts","8+hearts","9+hearts","10+hearts","j+hearts","q+hearts","k+hearts","a+hearts","2+spades","3+spades","4+spades","5+spades","6+spades","7+spades","8+spades","9+spades","10+spades","j+spades","q+spades","k+spades","a+spades"]
playershand = []
dealerhand = []

for i in range (2):
    a = (random.choice(deck))
    print(a)
    deck.remove(a)
    playershand.append(a)
    

firstcard = playershand[0]
secondcard = playershand[1]
x = firstcard.split("+")
y = secondcard.split("+")

num1 = x[0]
num2 = y[0]

if num1 == ("2") or num1 == ("3") or num1 == ("4") or num1 == ("5") or num1 == ("6") or num1 == ("7") or num1 == ("8") or num1 == ("9") or num1 == ("10"):
    num1 = int(num1)
if num1 == ("j") or num1 == ("q") or num1 == ("k"):
    num1 = (10)
    num1 = int(num1)
if num1 == ("a"):
    num1 = (11)
    num1 = int(num1)
if num2 == ("2") or num2 == ("3") or num2 == ("4") or num2 == ("5") or num2 == ("6") or num2 == ("7") or num2 == ("8") or num2 == ("9") or num2 == ("10"):
    num2 = int(num2)
if num2 == ("j") or num2 == ("q") or num2 == ("k"):
    num2 = (10)
    num2 = int(num2)
if num2 == ("a"):
    num2 = (11)
    num2 = int(num2)

total = (num1 + num2)
print("The total is",total)

b = (random.choice(deck))
deck.remove(b)
c = (random.choice(deck))
deck.remove(c)
#to edit
print("The dealer has", b,c)

b = b.split("+")
c = c.split("+")

dnum1 = b[0]
dnum2 = c[0]

if dnum1 == ("2") or dnum1 == ("3") or dnum1 == ("4") or dnum1 == ("5") or dnum1 == ("6") or dnum1 == ("7") or dnum1 == ("8") or dnum1 == ("9") or dnum1 == ("10"):
    dnum1 = int(dnum1)
if dnum1 == ("j") or dnum1 == ("q") or dnum1 == ("k"):
    dnum1 = (10)
    dnum1 = int(dnum1)
if dnum1 == ("a"):
    dnum1 = (11)
    dnum1 = int(dnum1)
if dnum2 == ("2") or dnum2 == ("3") or dnum2 == ("4") or dnum2 == ("5") or dnum2 == ("6") or dnum2 == ("7") or dnum2 == ("8") or dnum2 == ("9") or dnum2 == ("10"):
    num2 = int(num2)
if dnum2 == ("j") or dnum2 == ("q") or dnum2 == ("k"):
    dnum2 = (10)
    dnum2 = int(dnum2)
if dnum2 == ("a"):
    dnum2 = (11)
    dnum2 = int(dnum2)

dnum1 = int(dnum1)
dnum2 = int(dnum2)
dtotal = dnum1 + dnum2

ans = input("Would you like another card? (y/n)")
if ans == "y":
    xcard = (random.choice(deck))
    print(xcard)
    print(deck)
    deck.remove(xcard)
    playershand.append(xcard)
    deck.remove(xcard)
    print (playershand)
    x = xcard.split("+")
    num = x[0]
    if num == ("2") or num == ("3") or num == ("4") or num == ("5") or num == ("6") or num == ("7") or num == ("8") or num == ("9") or num == ("10"):
        num = int(num)
    if num == ("j") or num == ("q") or num == ("k"):
        num = (10)
        num = int(num)
    if num == ("a"):
        num = (11)
        num = int(num)
        total = num1 + num2 + num
    print (total)
    if total > 21:
        print("BUST, you lose!")
Why is this return after asking for a another card, xcard not in list
Reply
#2
You need to put your code between python tags when posting.
Also, when you pick the new card, you try to remove it from the deck twice.
Reply
#3
I'm sure you found by now that you were removing xcard twice.

Why are you surrounding this code with ()?
b = (random.choice(deck))
This would make a tuple if there were more than one card drawn.
b = (random.choice(deck), random.choice(deck))
print(b)
Output:
('8+spades', 'j+spades')
You would have quickly discovered this if Python didn't have a strange requirement that an initializer for a tuple with a single value has to have a trailing comma
this_is_a_string = ('some text')
this_is_a_tuple = ('some text',)
I bet you are getting really tired of typing this:
firstcard = playershand[0]
secondcard = playershand[1]
x = firstcard.split("+")
y = secondcard.split("+")
 
num1 = x[0]
num2 = y[0]
 
if num1 == ("2") or num1 == ("3") or num1 == ("4") or num1 == ("5") or num1 == ("6") or num1 == ("7") or num1 == ("8") or num1 == ("9") or num1 == ("10"):
    num1 = int(num1)
if num1 == ("j") or num1 == ("q") or num1 == ("k"):
    num1 = (10)
    num1 = int(num1)
if num1 == ("a"):
    num1 = (11)
    num1 = int(num1)
if num2 == ("2") or num2 == ("3") or num2 == ("4") or num2 == ("5") or num2 == ("6") or num2 == ("7") or num2 == ("8") or num2 == ("9") or num2 == ("10"):
    num2 = int(num2)
if num2 == ("j") or num2 == ("q") or num2 == ("k"):
    num2 = (10)
    num2 = int(num2)
if num2 == ("a"):
    num2 = (11)
    num2 = int(num2)
Python has a lot of neat features that make writing this kind of code mush shorter. Here's a shorter way to make a shuffled deck of cards and dealing two hands.
import random
rank = {'2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, \
        '10':10, 'J':10, 'Q':10, 'K':10, 'A':11}
suit = {'C':'Clubs', 'D':'Diamonds', 'H':'Hearts', 'S':'Spades'}
deck = [r + s for s in suit for r in rank]

random.shuffle(deck)
playerhand = [deck.pop(), deck.pop()]
dealerhand= [deck.pop(), deck.pop()]
rank and suit are dictionaries, something you need to learn about to write Python programs. deck is a list comprehension, a shorthand way for making lists that is usually more efficient than using loops.
pop() removes the first card in the deck. It combines "card = deck[0] and deck.remove(card)".

Use functions for tasks you perform more than once or twice. To get the value of a card:
def cardvalue(card):
    """Return numerical value of card"""
    return rank[card[:-1]]
If card is '10S', this gets the substring '10' and returns the associated value 10 from the rank dictionary. If it was 'AC' it would look up 'A' and return 11.

If you don't like names like '2C' or 'KS' write a function to make pretty names:
ranknames = {'2':'Duece', '3':'Three', '4':'Four', '5':'Five', '6':'Six', \
             '7':'Seven', '8':'Eight', '9':'Nine', '10':'Ten', 'J':'Jack', \
             'Q':'Queen', 'K':'King', 'A':'Ace'}

def cardname(card):
    return f'{ranknames[card[:-1]]} of {suit[card[-1]]}'
What you do not want to do, and I cannot stress this enough, is write the same single use code over and over.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Finding string in list item jesse68 8 1,858 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  how to easily create a list of already existing item CompleteNewb 15 3,526 Jan-06-2022, 12:48 AM
Last Post: CompleteNewb
  Remove an item from a list contained in another item in python CompleteNewb 19 5,666 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  count item in list korenron 8 3,433 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Time.sleep: stop appending item to the list if time is early quest 0 1,869 Apr-13-2021, 11:44 AM
Last Post: quest
  How to run a pytest test for each item in a list arielma 0 2,366 Jan-06-2021, 10:40 PM
Last Post: arielma
  How do I add a number to every item in a list? john316 2 1,964 Oct-28-2020, 05:29 PM
Last Post: deanhystad
  Ignoring a list item hank4eva 2 2,106 Aug-17-2020, 08:40 AM
Last Post: perfringo
  Select correct item from list for subprocess command pythonnewbie138 6 3,290 Jul-24-2020, 09:09 PM
Last Post: pythonnewbie138
  best way to add item to list only once Phaze90 1 2,516 May-01-2020, 05:02 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020