0

So Im trying to write code for an online blackjack program and I am having trouble with programming hitting, as I need to calculate using up to 11 variables that are drawn from a list randomly. Right now, my code for that section is:

while user_card_total_one <= 21:
    user_move = input("Would you like to stand or hit? ")
    user_move = user_move.lower()
    if user_move == "stand":
        break
    elif user_move == "hit":
        user_card_three = random.choice(deck)
        deck.remove(user_card_three)
        a_or_an_one = " a "
        if user_card_three == 8 or user_card_three == "Ace":
            a_or_an_one = " an "
        print("You were dealt" + a_or_an_one + str(user_card_three) + ".")
        if type(user_card_three) == str and user_card_one != "Ace":
            user_card_one = 10
        if user_card_three == "Ace":
            user_card_total_one = user_card_total_one + 1
            user_card_total_two = user_card_total_two + 11
            if user_card_total_two > 21:
                print("Your card total is " + str(user_card_total_one) + ".")
            else:
                print("Your card total is " + str(user_card_total_one) + " or " + str(user_card_total_two) + ".")
        else:
            user_card_total_one = user_card_total_one + user_card_three
            user_card_total_two = user_card_total_two + user_card_three
            print("Your card total is " + str(user_card_total_one) + ".")
        if user_card_total_one > 21:
            print("Unfortunately, you busted and lost.")
            print("The dealer has collected your bet.")
            bust = True
            break
    else:
        print("Sorry, thats not a valid input, please try again.")

Now, I could write this code a bunch more times so that it accounts for what the user decides to do, but the only change in the code would be the variable name. I was wondering if there is some way that I can make this a for loop where perhaps the variable name is dependent on i? I tried using a dictionary:

card = {
    1: "Ace",
    2: "King",
    3: 2
}
card = 1
for i in range (10):
    print(card[card])
    card = card+1

But that didn't seem to help. Is this something I just have to brute force, or is there an easier way that Im missing?

Edit: Sorry, I used some wrong code in the second section, it's updated now

martineau
  • 119,623
  • 25
  • 170
  • 301
  • You know right that You might as well have used the `i` instead of creating another variable for counting? also what exactly are You trying to accomplish? Your last bit of code seems to just print out the dictionary values so it is hard to understand what You want. – Matiiss May 10 '21 at 21:01
  • 1
    Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – Shivam Roy May 10 '21 at 21:03
  • 1
    Dont do this, don't dynamically create variables. Use a *container* like a list or a dict – juanpa.arrivillaga May 10 '21 at 21:18

1 Answers1

0

I believe you want to use the iterator in the naming of a key within a dictionary? how's this look?

length=10
cardDict=dict()

for i in range(length):
    objectname = "Card "+str(i)
    cardDict[objectname]="number "+str(i)
print (cardDict)
Shawn Ramirez
  • 796
  • 1
  • 5
  • 10
  • Okay, great, that seems to work, even with pulling the variable from a random list: cardDict[objectname]=random.choice(deck) The only issue I run into is then removing the variable from the list. I used "deck.remove(objectname)", but maybe I just need to use something else or Im using the wrong function, because I get this error: ValueError: list.remove(x): x not in list If it helps, this is the deck Im using to test it: deck=[2,3,4,5,6,7,8,9,10,"Jack","Queen","King","Ace"] – MrParasite9445 May 10 '21 at 21:39
  • 1
    @MrParasite9445: You can delete an entry in a dictionary named `deck` via `del deck[objectname]`. You'll get a `KeyError` if it doesn't exist. And you check if something is in it with `if objectname in deck:`. – martineau May 10 '21 at 21:59