-1

I am a beginner programmer using Python 3.7 on Spyder, and when i run my code, it opens a system32 command prompt that immediately closes afterward. Nothing happens. The same thing happens when I try to press the debug button. I'll paste my code here. Edit: Thanks to oliverm, I have updated the code, but the code still will not start.

    # -*- coding: utf-8 -*-
    """
    Spyder Editor
    
    This is a temporary script file.
    """
    #imports
    import random
    #def the vars
    credits = 10
    playagain = None
    jack = 10
    queen = 10
    king = 10
    ace = 11 #Nice
    x = 0
    y = 0
    a = 0
    b = 5
    playing = True
    cardamount = 0
    deck = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,jack,jack,jack,jack,queen,queen,queen,queen,king,king,king,king,ace,ace,ace,ace]
    #def the functions
    def greetings():
        print("Greetings! Weclome to Blackjack! d\Do you wish to play?")
        print ("Yes or No")
        answer = input()
        if answer == "Yes" or "yes" or "y" or "Y":
            blackjack()
        elif answer == "No" or "no" or "n" or "N":
            print ("Alright then. Have a good day!")
        else:
            print("I have no idea what you just said. can you try that again?")
            return
    def bets():
        print ("How much will you bet?")
        print ("You have ",credits, " credits")
        bet = int(input())
        print ("Alright! Lets start!")
        pass
    def playerplay():
        card1 = random.choice(deck)
        card2 = random.choice(deck)
        #ace logic here. an if statement.
        cardlist = [card1, card2]
        if card1 == ace and card2 == ace:
            pastace = 2
            card1 = 1
            card2 =11
        elif card1 == ace:
            pastace = 1
            if card2 + 11 > 21:
                ace = 1
            else:
                ace = 11
        elif card2 == ace:
            pastace = 1
            if card1 + 11 > 21:
                ace = 1
            else:
                ace = 11
        else:
            pastace = 0
            pass
        cardamount = card1 + card2
        print ("You drew a ", card1, " and a ", card2)
        while playing == True:
            print ("You currently have ", cardamount, " points with ", pastace, "aces" )
            print ("Hit or Stand?")
            hvalue = str(input())
            if hvalue == 'Hit' or "hit" or "H" or "h":
                cardlist.append(random.choice(deck))
                #calculates cardamount
                for y in len(cardlist):
                    cardamount += cardlist[y]
                    if y == len(cardlist):
                        y = 0
                        break
                    else:
                        y += 1
                #paste ace logic here
                if cardlist[x + 1] == ace:
                    pastace += 1
                if cardamount > 21 and pastace == 1:
                    ace = 1
                    cardamount == card1 + card2
                    for y in len(cardlist):
                        cardamount += cardlist[y]
                        if y == len(cardlist):
                            y = 0
                            break
                        else:
                            y += 1
                    if cardamount > 21:
                        print("You have drawn over 21 cards. Now the dealer will play.")
                        playing = False
                        del cardlist[0:x]
                        pcardamount = cardamount
                        pastace = 0
                        x = 0
                        pass
                    else:
                        x += 1
                        return
                elif cardamount > 21 and pastace > 1:
                    ace = 1
                    cardamount = card1 + card2
                    for y in len(cardlist):
                        cardamount += cardlist[y]
                        if y == len(cardlist):
                            y = 0
                            break
                        else:
                            y += 1
                    if cardamount > 21:
                        print("You have drawn over 21 cards. Now the dealer will play.")
                        playing = False
                        del cardlist[0:x]
                        pastace = 0
                        pcardamount = cardamount
                        x = 0
                        pass
                    elif cardamount - 22 > -11:
                        ace in cardlist = 11
                        return
                    else:
                        x += 1
                        return
                else:
                    x += 1
                    return
            elif hvalue == 'Stand' or 'stand' or 'S' or "s":
                print ('Alright! Your total card amount is ', cardamount)
                playing = False
                del cardlist[0:x]
                pastace = 0
                pcardamount = cardamount
                x = 0
                pass
            else:
                print ("I'm sorry. What did you say?")
                return
    def outcome():
        print("Your total card amount was", pcardamount, "and the dealer's was ", dcardamount)
        if pcardamount > dcardamount and pcardamount <= 21 :
            print("You win!")
            credits += bet*2
            
        elif pcardamount < dcardamount and dcardamount <=21 :
            print("You lose...")
            credits -= bet
        else:
            print("It's a tie!")
        #says who wins here and defines playagain
        playing = True
        print("You now have ", credits, " credits.")
        print ("Do you wish to play again?")
        answer2 = str(input())
        if answer2 == "Yes" or "yes" or "y" or "Y":
            blackjack()
        elif answer2 == "No" or "no" or "n" or "N":
            print("Thank you for playing! Have a great day!")
        else:
            print: ("I have no idea what you just said. can you try that again?")
            return
    def hit():
        cardlist.append(random.choice(deck))
        #calculates cardamount
        for y in len(cardlist):
            cardamount += cardlist[y]
            if y == len(cardlist):
                y = 0
                break
            else:
                y += 1
        #paste ace logic here
        if cardlist[x + 1] == ace:
            pastace += 1
        if cardamount > 21 and pastace == 1:
            ace = 1
            cardamount == card1 + card2
            for y in len(cardlist):
                cardamount += cardlist[y]
                if y == len(cardlist):
                    y = 0
                    break
                else:
                    y += 1
            if cardamount > 21:
                del cardlist[0:x]
                pastace = 0
                x = 0
                dcardamount = cardamount
                cardamount = 0
                pass
            else:
                x += 1
                return
        elif cardamount > 21 and pastace > 1:
            ace = 1
            cardamount = card1 + card2
            for y in len(cardlist):
                cardamount += cardlist[y]
                if y == len(cardlist):
                    y = 0
                    break
                else:
                    y += 1
            if cardamount > 21:
                playing = False
                del cardlist[0:x]
                pastace = 0
                x = 0
                dcardamount = cardamount
                cardamount = 0
                pass
            elif cardamount - 22 > -11:
                ace in cardlist = 11
                return
            else:
                x += 1
                return
        else:
            x += 1
    def dealerplay():
        card1 = random.choice(deck)
        card2 = random.choice(deck)
        #ace logic here. an if statement.
        cardlist = [card1, card2]
        if card1 == ace and card2 == ace:
            pastace = 2
            card1 == 1
            card2 ==11
        elif card1 == ace:
            pastace = 1
            if card2 + 11 > 21:
                ace = 1
            else:
                ace = 11
        elif card2 == ace:
            pastace = 1
            if card1 + 11 > 21:
                ace = 1
            else:
                ace = 11
        else:
            pastace = 0
            pass
        cardamount = card1 + card2
        if cardamount < 17:
            hit()
            return
        else:
            del cardlist[0:x]
            pastace = 0
            x = 0
            dcardamount = cardamount
            cardamount = 0
            pass
    def blackjack():
        bets()
        playerplay()
        dealerplay()
        outcome()
    
    #game functions put together
    greetings()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Big Bird
  • 7
  • 1
  • 4
  • _I'm trying to run it so i can see the errors I get_ Don't you already know the errors, though? – AMC Dec 18 '19 at 18:36
  • Edit: I have no more inconsistent use of tabs in indentation, but now i am getting 'SyntaxError: can't assign to comparison.' for the line that says `ace in cardlist = 11` – Big Bird Dec 19 '19 at 20:32
  • Where is that? What do you expect that expression to do? – AMC Dec 19 '19 at 20:41
  • It is in line 126 and 220, and I want it to make only the first ace in cardlist equal to eleven while the other ace(s) equal 1. – Big Bird Dec 22 '19 at 20:24
  • That code is extremely dense, I'm struggling to read it. – AMC Dec 23 '19 at 01:08

1 Answers1

1

Regarding:

'inconsistent use of tabs in indentation.'

You'll need to make sure to either use tabs OR simple space as white space characters. Mixing both will result in the error above. You can configure your spyder to use e.g. 4 intends when pressing tab.

As per the answer of Spyder Python indentation try

In Spyder v3.0.0, go to Source --> Fix Indentation. That worked for me.

Before you can start debugging you need to fix your syntactic errors.

To use the debugger you'll need to define a breakpoint within your code or when starting the debugger. Spyder uses the ipdb debugger within the ipython console. Have a look at the spyder docs about debugging: https://docs.spyder-ide.org/debugging.html

From their docs you have the following options to set a breakpoint:

Multiple means of setting and clearing normal and conditional breakpoints for any line in a file opened in the Editor.

  • By selecting the respective option from the Debug menu.
  • Through pressing a configurable keyboard shortcut (F12 for normal, or Shift-F12 for conditional breakpoints by default).
  • By double-clicking to the left of the line number in an open file.
  • With an ipdb.set_trace() statement in your code (after import ing pdb).
  • Interactively, using the b command in an ipdb session.

Have a look at e.g.:https://realpython.com/python-debugging-pdb/ for a quick debugging tutorial.

I've quickly fixed your syntactic problems in the code below. In addition, the if clause for choosing to play or not to play are changed to correctly evaluated to True or False if input is passed.

Debugging should now be possible. To get you started I've imported the ipdb at the beginning of your script and set a breakpoint in the greetings function (line 29).

Regarding your problem with the window closing, check what is set in Tools -> Preferences -> Run -> Console. If you want the program to execute in the current iPython console of Spyder select "Execute in current console".

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
#imports
import random
import ipdb
#def the vars
credits = 10
playagain = None
jack = 10
queen = 10
king = 10
ace = 11 #Nice
x = 0
y = 0
a = 0
b = 5
playing = True
cardamount = 0
deck = [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,jack,jack,jack,jack,queen,queen,queen,queen,king,king,king,king,ace,ace,ace,ace]
# def the functions


def greetings():
    ipdb.set_trace()
    print("Greetings! Weclome to Blackjack! \n Do you wish to play?")
    print("Yes or No")
    answer = input()
    if answer in ["Yes", "yes", "y", "Y"]:
        blackjack()
    elif answer in ["No", "no", "n", "N"]:
        print("Alright then. Have a good day!")
        return
    else:
        print("I have no idea what you just said. can you try that again?")
        return


def bets():
    print("How much will you bet?")
    print("You have ", credits, " credits")
    bet = int(input())
    print("Alright! Lets start!")
    return bet


def playerplay():
    card1 = random.choice(deck)
    card2 = random.choice(deck)
    #ace logic here. an if statement.
    cardlist = [card1, card2]
    if card1 == ace and card2 == ace:
        pastace = 2
        card1 = 1
        card2 =11
    elif card1 == ace:
        pastace = 1
        if card2 + 11 > 21:
            ace = 1
        else:
            ace = 11
    elif card2 == ace:
        pastace = 1
        if card1 + 11 > 21:
            ace = 1
        else:
            ace = 11
    else:
        pastace = 0
        pass
    cardamount = card1 + card2
    print ("You drew a ", card1, " and a ", card2)
    while playing == True:
        print ("You currently have ", cardamount, " points with ", pastace, "aces" )
        print ("Hit or Stand?")
        hvalue = str(input())
        if hvalue == 'Hit' or "hit" or "H" or "h":
            cardlist.append(random.choice(deck))
            #calculates cardamount
            for y in len(cardlist):
                cardamount += cardlist[y]
                if y == len(cardlist):
                    y = 0
                    break
                else:
                    y += 1
            #paste ace logic here
            if cardlist[x + 1] == ace:
                pastace += 1
            if cardamount > 21 and pastace == 1:
                ace = 1
                cardamount == card1 + card2
                for y in len(cardlist):
                    cardamount += cardlist[y]
                    if y == len(cardlist):
                        y = 0
                        break
                    else:
                        y += 1
                if cardamount > 21:
                    print("You have drawn over 21 cards. Now the dealer will play.")
                    playing = False
                    del cardlist[0:x]
                    pcardamount = cardamount
                    pastace = 0
                    x = 0
                    pass
                else:
                    x += 1
                    return
            elif cardamount > 21 and pastace > 1:
                ace = 1
                cardamount = card1 + card2
                for y in len(cardlist):
                    cardamount += cardlist[y]
                    if y == len(cardlist):
                        y = 0
                        break
                    else:
                        y += 1
                if cardamount > 21:
                    print("You have drawn over 21 cards. Now the dealer will play.")
                    playing = False
                    del cardlist[0:x]
                    pastace = 0
                    pcardamount = cardamount
                    x = 0
                    pass
                elif cardamount - 22 > -11:
                    ace in cardlist == 11
                    return
                else:
                    x += 1
                    return
            else:
                x += 1
                return
        elif hvalue == 'Stand' or 'stand' or 'S' or "s":
            print ('Alright! Your total card amount is ', cardamount)
            playing = False
            del cardlist[0:x]
            pastace = 0
            pcardamount = cardamount
            x = 0
            pass
        else:
            print ("I'm sorry. What did you say?")
            return
def outcome():
    print("Your total card amount was", pcardamount, "and the dealer's was ", dcardamount)
    if pcardamount > dcardamount and pcardamount <= 21 :
        print("You win!")
        credits += bet*2

    elif pcardamount < dcardamount and dcardamount <=21 :
        print("You lose...")
        credits -= bet
    else:
        print("It's a tie!")
    #says who wins here and defines playagain
    playing = True
    print("You now have ", credits, " credits.")
    print ("Do you wish to play again?")
    answer2 = str(input())
    if answer2 in ["Yes", "yes", "y", "Y"]:
        blackjack()
    elif answer2 in ["No", "no", "n", "N"]:
        print("Alright then. Have a good day!")
        return
    else:
        print("I have no idea what you just said. can you try that again?")
        return

def hit():
    cardlist.append(random.choice(deck))
    #calculates cardamount
    for y in len(cardlist):
        cardamount += cardlist[y]
        if y == len(cardlist):
            y = 0
            break
        else:
            y += 1
    #paste ace logic here
    if cardlist[x + 1] == ace:
        pastace += 1
    if cardamount > 21 and pastace == 1:
        ace = 1
        cardamount == card1 + card2
        for y in len(cardlist):
            cardamount += cardlist[y]
            if y == len(cardlist):
                y = 0
                break
            else:
                y += 1
        if cardamount > 21:
            del cardlist[0:x]
            pastace = 0
            x = 0
            dcardamount = cardamount
            cardamount = 0
            pass
        else:
            x += 1
            return
    elif cardamount > 21 and pastace > 1:
        ace = 1
        cardamount = card1 + card2
        for y in len(cardlist):
            cardamount += cardlist[y]
            if y == len(cardlist):
                y = 0
                break
            else:
                y += 1
        if cardamount > 21:
            playing = False
            del cardlist[0:x]
            pastace = 0
            x = 0
            dcardamount = cardamount
            cardamount = 0
            pass
        elif cardamount - 22 > -11:
            ace in cardlist == 11
            return
        else:
            x += 1
            return
    else:
        x += 1
def dealerplay():
    card1 = random.choice(deck)
    card2 = random.choice(deck)
    #ace logic here. an if statement.
    cardlist = [card1, card2]
    if card1 == ace and card2 == ace:
        pastace = 2
        card1 == 1
        card2 ==11
    elif card1 == ace:
        pastace = 1
        if card2 + 11 > 21:
            ace = 1
        else:
            ace = 11
    elif card2 == ace:
        pastace = 1
        if card1 + 11 > 21:
            ace = 1
        else:
            ace = 11
    else:
        pastace = 0
        pass
    cardamount = card1 + card2
    if cardamount < 17:
        hit()
        return
    else:
        del cardlist[0:x]
        pastace = 0
        x = 0
        dcardamount = cardamount
        cardamount = 0
        pass
def blackjack():
    bets()
    playerplay()
    dealerplay()
    outcome()

#game functions put together
greetings()
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
oliverm
  • 113
  • 8
  • a brakepoint as in `break`? and when i press tab, it indents twice on line 69. one indent is a tab indent, the other is four spaces. – Big Bird Dec 18 '19 at 15:28
  • All your indents in the whole file need to be of the same type. Either taps or indents not both mixed. Please have a look at the link. Breakpoints are not related to brake in E. G. Loops – oliverm Dec 18 '19 at 15:35
  • 1
    A breakpoint allows the programmer to "pause" the program when it gets to a specific line. It then allows you to inspect the values of the variables in your code. This is very helpful while debugging logical errors in your program - however you need to fix your syntax first. Also, with the error you talked about in your question - that will go away if you make sure not to mix spaces and tabs in the same file. Use one or the other everywhere. Find and replace will help you with this a lot! – Theo Emms Dec 18 '19 at 15:49
  • I added a breakpoint to the end of my code, but it still wouldn't start. and also, i cannot repalce spaces with tabs in the find and replace, nor the opposite. – Big Bird Dec 18 '19 at 18:47
  • @BigBird I've edited the answer to show how to replace whitespace and added a link to a python debugging tutorial. – oliverm Dec 19 '19 at 10:45
  • I did that and it solved that problem! Thanks. When i tried to run it again, it still did the thing where it opens a command window and immeadeatley closes it. it is a system32 prompt. Do you know how to fix that? – Big Bird Dec 19 '19 at 14:49
  • @BigBird My answer should now contain everything you need to get started. If that solved your problem please consider accepting the answer. – oliverm Dec 19 '19 at 15:27
  • Thank you for doing that. However, i am getting lots of warnings when i pasted the code in. It also did the same thing with the system 32 command prompt. – Big Bird Dec 19 '19 at 16:04
  • You need to clarify on your warnings, if you are talking about yellow marks in spyder these are shown because you have a lot of coding standard violations which are shown after all syntax errors are fixed. If you have tried what I've written about your terminal I can not help you without further information. – oliverm Dec 19 '19 at 16:10