r/pythonhelp • • Jan 04 '21

I’m making a blackjack script but code can’t use “>” . Changing my inputs to int(input hasn’t worked. I checked and it does add up the numbers, for example if I get a 9 of hearts, 7 of diamonds it correctly adds to 16 but I still get the error. Can someone help?

Post image
3 Upvotes

11 comments sorted by

2

u/socal_nerdtastic Jan 04 '21

You are right that you need to do an int() conversion, but it's not your inputs, its your file read that needs to be converted.

If you need help with that Post your code as text so that we can fix it and test it.

1

u/MXP04 Jan 04 '21

Is there some way to type the code here without doing the method? I’ve tried the method so many times and always made a a mistake somewhere and this code is really long

1

u/socal_nerdtastic Jan 04 '21

Not sure what you mean with the method. Here's how to format your code for reddit: https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F or use a site like github or pastebin.

1

u/MXP04 Jan 04 '21

The bits which are hashtagged I did to show which isn’t working

1

u/socal_nerdtastic Jan 04 '21

Hmm what does your data file look like?

1

u/MXP04 Jan 04 '21

Turned my computer off but it was basically just each card on its own line so like : “2 of hearts” (new line) “3 of hearts” (new line) “4 of hearts” (new line) Etc... (Without the speech marks tho)

1

u/socal_nerdtastic Jan 04 '21

So why do you have a split(', ') in there? It looks like there's more data on each line.

1

u/MXP04 Jan 04 '21

Sorry I just realised I forgot to mention thag there was commas after each card. My bad. I used it so that the program knew that it was a different part to the previous card

1

u/socal_nerdtastic Jan 04 '21

So no extra data? just a comma?

2 of hearts,
3 of hearts,
4 of hearts,

Then you need to strip the comma, not split it. Once you do that you can add the integer conversion I mentioned at the start:

f = open("cards.txt","r")
lines = f.readlines()

cardarray = [] # TECHNICALLY THIS IS A LIST, NOT AN ARRAY
for line in lines:
  cardarray.append(line.strip().strip(","))
f.close()

cardvalues = []
for card in cardarray:
    # HERE WE CONVERT EACH CARD TO AN INT VALUE, TO ALLOW SUMMING AND COMPARISON
    cardvalues.append(int(card.split()[0]))

Once you do that you can forget all of the [0]s in your code. Use cardarray to get the full name of the card:

print("You got the ", cardarray[x])

And cardvalues when you want to work with the values only:

if (cardvalues[x] + cardvalues[y]) == 21:

This will fix your original error and a lot of other errors you never knew you had.

1

u/MXP04 Jan 04 '21

I did those changes, and for most part it worked, but some didn’t. I DM’ed you a link that will allow u to directly edit my code instead of us having to constantly write back and forth to each other