Python Forum
Iteration, selection and adding - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Iteration, selection and adding (/thread-26671.html)



Iteration, selection and adding - gandy - May-09-2020

First post here.
I'm trying to get the value of the cards in PlayersHand and add them.Then 'hit' a new card and add it to variable 'score'.
I've seen lots of options, including dicts and splits, but they don't make sense fully to me yet.
Is there a way using the (i) iteration to add the i's?
Thanks
I've just included a section of the code

if option == 1:
        print ("Lets Play!")
        
          #+++++++++++++++++Deal and display cards++++++++++++++++#
        PlayersHand = random.choices(Deck, k=2)
        print(name,"Your cards are", PlayersHand)
        DealersHand = random.choices(Deck, k=1)
        print("Dealer, your cards are Blank +",DealersHand)
        total = 0
        for i in PlayersHand:
            countval=(i[0])#to get the value of the card(needed first two values for one or ten
            print ("count is", countval);
            print("-------------")
            print(i)
        if countval == "J" or countval =="K" or countval =="Q": total +=10
        elif  countval =="A":
                total = 11
        else:
                total=countval
        
        print ("countval is",countval)
        print ("countval is",total)    
        print (PlayersHand)
#        print(PlayersHand.numval)


        #+++++++++++++++++Turn value into integer++++++++++++++++#
        def card_value(card):
    

            rank = Deck[0]
            if rank in Ranks[0:-4]:
                return int(rank)
            elif rank == 'ACE':
                return 11
            else:
                return 10
                print("card")
                print (rank)
                
        while sum(PlayersHand) <21:
            decide = input("Do you want to hit or stand?: H or S?")
            if decide == "H" or "h":
                PlayersHand.append(random.choices(Deck, k=1))



RE: Iteration, selection and adding - DPaul - May-09-2020

You have many questions, i think a start would be to put your deck in a dictionary type object like so:
deck = {'HA':11,'H2':2,'H3':3,'HJ':10}
print(deck['H2'])
I've included a few cards like Hearts/Ace, H 2, H 3, H Jack.... with the values they would have in blackjack.
With the key eg. 'HA', you can find the value and that leads to adding, etc.
Paul