#🔒 Learning Python and trying to build a Blackjack game as practice, function returning Nonetype

76 messages · Page 1 of 1 (latest)

zealous lilyBOT
#

@young cypress

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

young cypress
#

Well rip gotta type it all out again hahaha.

I am currently learning Python at a power rate.

Currently I am practicing by creating a blackjack game and it is SO nearly complete but one small issue is creeping up. One of my functions is returning NoneType but I cannot work out for the life of me why it might be doing it. (Any code critique and recommendations would be appreciated also!)

#

Now I BELEIVE the issue lies somehow within the DealerCheck() function as its when it returns it becomes NoneType but not before or during the function as far as I can see.

#

I suspect I am perhaps misunderstanding something fundamental about python

#

Okay now I am more confused, for the first time Ive had the PLAYER score come back as NoneType sooooo.... Unsure 🥲

cinder verge
#

In your function DealerCheck, you dont return anything if the dealerscore is <17, but you always expect a return

sly tulip
#

I don't see baseDeck defined anywhere so its not clear where that is coming from

young cypress
young cypress
#

DealerCheck(dealerScore)

golden geyser
#

Which function is returning NoneType?

#

Paste your code and the traceback please.

cinder verge
sly tulip
young cypress
young cypress
young cypress
# golden geyser Paste your code and the traceback please.
The aim of the game is to make 21 with your cards without going over.
You will go first, and dealer will go second.
The Dealer MUST always choose hit unless they are at 17 or above in which case they MUST stand.
In this version of the game, a Blackjack is an automatic win unless both players get one.
Ace is always 1 unless within a Blackjack (Starting hand of 21)
Are you ready? Y/N: Y
You drew K♦
The Dealer drew
score is checked
You drew 5♦
The Dealer drew
Your cards are: ['K♦', '5♦']
The Dealers first card is: 3♦
score is checked
score is checked
score is checked
score is checked
Your current score is: 15
The Dealers score is: 13
Hit or Stand?: stand
<class 'int'>
Your score is 15
Would you like to Hit or Stand?: stand
You stand on 15
<class 'int'>
The Dealer Draws 10â™ 
score is checked
The Dealers Score is 23
<class 'int'>
The Dealer is bust at 23
dealerScore final
<class 'NoneType'>
playerScore final
<class 'int'>
Traceback (most recent call last):
  File "d:\webdevprojects\Python Course\CardGames.py", line 260, in <module>
    SelectAGame()
  File "d:\webdevprojects\Python Course\CardGames.py", line 19, in SelectAGame
    BlackJack()
  File "d:\webdevprojects\Python Course\CardGames.py", line 255, in BlackJack
    SelectAGame()
  File "d:\webdevprojects\Python Course\CardGames.py", line 19, in SelectAGame
    BlackJack()
  File "d:\webdevprojects\Python Course\CardGames.py", line 250, in BlackJack
    CheckWin(playerScore, dealerScore)
  File "d:\webdevprojects\Python Course\CardGames.py", line 189, in CheckWin
    elif playerScore <= 21 and dealerScore > 21:
                               ^^^^^^^^^^^^^^^^
TypeError: '>' not supported between instances of 'NoneType' and 'int'```
#

the lines on this will be different to the pastebin

golden geyser
#

score = int(0)

#

why?

young cypress
#

Another attempt at trying to figure out where the datatype issue is coming from

golden geyser
#

Is dealerScore defined after line 130 in the paste?

young cypress
#

It wouldnt undefine itself would it?

golden geyser
#

no but the CheckScore might return NoneType

#

let me read the code closer

young cypress
sly tulip
#

Shouldn't the last line of ```py
def DealerCheck(dealerScore):
if dealerScore > 21:
print("The Dealer is bust at "+ str(dealerScore))
return dealerScore
elif dealerScore >= 17:
print("The dealer stands at " + str(dealerScore))
return dealerScore
elif dealerScore < 17:
x = random.choice(baseDeck)
print("The Dealer Draws "+ x)
dealerScore = dealerScore + CheckScore(x)
print("The Dealers Score is " + str(dealerScore))
DealerCheck(dealerScore)

#

be py return DealerCheck(dealerScore)

young cypress
#

Would return be necessary here?

#

Since its not returning anything to the original class just running the class again

#

with the new value

golden geyser
#

that's not a class, it's a function

sly tulip
#

you check the score here dealerScore = DealerCheck(dealerScore)

young cypress
sly tulip
#

and the result is less than 17 so it goes to the last statement and calls itself

#

and then returns when its a bust to itself but the main calling function never gets a return

#

Try it and see

young cypress
#

Shouldnt it only return when the conditoons are satisfied?

#

and if that section is running then the conditions arent met

sly tulip
#

!e ```py

def foo(x):
if x > 5:
return True
else:
x+=1
foo(x)

y = foo(1)
print(y)```

zealous lilyBOT
#

@sly tulip :white_check_mark: Your 3.12 eval job has completed with return code 0.

None
sly tulip
#

see ^

young cypress
#

Interesting

sly tulip
#

!e ```py

def foo(x):
if x > 5:
return True
else:
x+=1
return foo(x)

y = foo(1)
print(y)```

zealous lilyBOT
#

@sly tulip :white_check_mark: Your 3.12 eval job has completed with return code 0.

True
young cypress
#

Im not getting any errors now

#

I dont understand "why" a return should be needed in that scenario since the function hasn't passed back to the call yet

#

Wel its awesome that its working, But i definately do not like that I dont understand why xD if you have any way to explain it I would apprecaite it

sly tulip
#

Let me see if I can find a resource that can explain better

young cypress
#

Thanks, I apprecaite it

#

Oof time to dive back in

#

My score should NOT be 2 xD

sly tulip
#

This would probably be helpful https://realpython.com/python-recursion/. My understanding of it is essentially it creats a namespace when you call your function. When it goes through the conditions and recursively calls itself it creates another namespace where the value is returned to that original call. But since theres no return statement your first call just gets None back.

In this tutorial, you'll learn about recursion in Python. You'll see what recursion is, how it works in Python, and under what circumstances you should use it. You'll finish by exploring several examples of problems that can be solved both recursively and non-recursively.

young cypress
young cypress
#

Ill take a look thank you.

sly tulip
young cypress
#

Thanks 🙂

#

Alrighty Now to deal with Ace 1/11 =_=

#

(currently it only scores 1)

sly tulip
#

you should make an enum class for your base deck

#

Then you can set your cards Display str with its corresponding value

sly tulip
young cypress
sly tulip
#

Its basically the same thing

young cypress
#

Ive never done an enum class, something to look into

sly tulip
#

personally I think enums are more structured and easier to read and use

young cypress
#

Im gonna head off now, but Ill add it to my notes and check it out tomorrow. Thank you!

#

I think Ive made some good progress with learning Python in what 2/3 days so far x3

#

Definately helps that i have knowledge of c#/++ etc but honestly I think I already know more about python than those kek

zealous lilyBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.