Python Forum
Iteration, selection and adding
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iteration, selection and adding
#1
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))
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding markers to Folium map only adding last element. tantony 0 2,140 Oct-16-2019, 03:28 PM
Last Post: tantony

Forum Jump:

User Panel Messages

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