0

I am currently working on a blackjack game without graphics or pygame I can not seem to make an if statement with input, I took a month off of python and can't really understand the questions already answered.

Here is my code so far:

def pullCard():
    print(f"You pulled a {r} of {s} the value is {v}")
    print(f"The dealer pulled a {dr} of {ds} the value is {dv}")

pullCard()

Answer = input('Do you want to Hit or Stand?')

if Answer == 'Stand' or 'stand':
    dealerOnly()
if Answer == 'Hit' or 'hit':
    pullCard()

I have not defined dealerOnly() every time i say Hit or Stand it just comes out with the error.

Do you want to Hit or Stand?Hit
Do you want to Hit or Stand?
Traceback (most recent call last):
  File "C:\Users\Brody Critchlow\Downloads\Blackjack\Blackjack.py", line 36, in <module>
    dealerOnly()
NameError: name 'dealerOnly' is not defined

Even though i said Hit not Stand

Brody Critchlow
  • 45
  • 3
  • 21

2 Answers2

2

your if statement is incorrect it should be

if Answer == 'Stand' or Answer == 'stand': 

A cleaner approach is to make the user input string all lower case, that way you only need to check one scenario.

def pullCard():
    print(f"You pulled a {r} of {s} the value is {v}")
    print(f"The dealer pulled a {dr} of {ds} the value is {dv}")

pullCard()

Answer = str(input('Do you want to Hit or Stand?')).lower()

    if Answer == 'stand':
        dealerOnly()
    elif Answer == 'hit':
        pullCard()

I also used an elif statement to make it more efficient so that if the first statement is true, then it won't run the second.

jun
  • 540
  • 5
  • 17
  • Thank you so much, and for answering quickly. I use Stackoverflow often usually I have to wait for days. I love my experience so far. I have not to meet any rude people! – Brody Critchlow Sep 24 '19 at 02:09
1

You have to understand that python uses truthy and falsey values, it means, that not only True and False are true and false, also strings and lists and other values, examples:

if "":
  print("wont enter, falsay value")

if("Hit"):
  print("Something, truthy value")

if []:
  print("wont enter, falsay value")

if [1,2,3]:
  print("Something, truthy value")

your main problem, is in this two expressions:

if Answer == 'Stand' or 'stand':
if Answer == 'Hit' or 'hit':

here, you use an or, it means that if anything is truthy, the block inside the if will be executed, and 'stand' and 'hit' are not an empty str, so it will always be executed because they are both True

Also, the other problem is that you have to make the two questions, Answer == "Stand" or Answer == "stand", as I don't have the methods, I will print something just to you can see what is called:

def pullCard():
    print("You pulled a {r} of {s} the value is {v}")
    print("The dealer pulled a {dr} of {ds} the value is {dv}")

pullCard()

Answer = input('Do you want to Hit or Stand?')

if Answer == 'Stand' or Answer == 'stand':
    print("dealerOnly()")
if Answer == 'Hit' or Answer == 'hit':
    print("pullCard()")

you can also simplify the if like this:

Answer = input('Do you want to Hit or Stand?').lower()

if Answer == 'stand':
    print("dealerOnly()")
if Answer == 'hit':
    print("pullCard()")
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • Thank you so much, and for answering quickly. I use Stackoverflow often usually I have to wait for days. I love my experience so far. I have not to meet any rude people! – Brody Critchlow Sep 24 '19 at 02:10
  • @PythonLuaMaster keep trying, stackoverflow is a hard place, because its ment to be a great web site, so older users cares a lot to the questions and answers to be useful and well formed, you keep and keep trying, listen to the comment, don't take them personal, and you will be an awesome user, try to answer questions as well, and don't forget to vote and accept one of the answers, the one that helped you most – developer_hatch Sep 24 '19 at 02:12
  • Okay! @Damián Rafael Lattenero Will do for sure. – Brody Critchlow Sep 25 '19 at 02:35