首页 > 解决方案 > 为什么 Spyder 不会运行或调试我的代码?

问题描述

我是在 Spyder 上使用 Python 3.7 的初学者程序员,当我运行我的代码时,它会打开一个 system32 命令提示符,然后立即关闭。什么都没发生。当我尝试按下调试按钮时也会发生同样的事情。我将在这里粘贴我的代码。编辑:感谢 Oliverm,我已经更新了代码,但代码仍然无法启动。

    # -*- 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()

标签: pythonpython-3.xdebuggingspyderipdb

解决方案


关于:

“缩进中制表符的使用不一致。”

您需要确保使用制表符或简单空格作为空白字符。将两者混合将导致上述错误。您可以将您的 spyder 配置为在按 Tab 时使用例如 4 个意图。

根据Spyder Python indentation try的回答

在 Spyder v3.0.0 中,转到 Source --> Fix Indentation。这对我有用。

在开始调试之前,您需要修复语法错误。

要使用调试器,您需要在代码中或在启动调试器时定义断点。Spyder 在 ipython 控制台中使用 ipdb 调试器。查看有关调试的 spyder 文档: https ://docs.spyder-ide.org/debugging.html

从他们的文档中,您可以使用以下选项来设置断点:

为在编辑器中打开的文件中的任何行设置和清除正常断点和条件断点的多种方法。

  • 通过从“调试”菜单中选择相应的选项。
  • 通过按可配置的键盘快捷键(默认为 F12 正常,或 Shift-F12 为条件断点)。
  • 通过双击打开文件中行号的左侧。
  • 在代码中使用 ipdb.set_trace() 语句(在 import pdb 之后)。
  • 以交互方式,在 ipdb 会话中使用 b 命令。

查看例如:https ://realpython.com/python-debugging-pdb/以获取快速调试教程。

我已经在下面的代码中快速修复了您的语法问题。此外,如果输入通过,用于选择播放或不播放的 if 子句更改为正确评估为 True 或 False。

现在应该可以进行调试了。为了让您开始,我在脚本的开头导入了 ipdb,并在 greetings 函数中设置了一个断点(第 29 行)。

关于关闭窗口的问题,请检查工具 -> 首选项 -> 运行 -> 控制台中的设置。如果您希望程序在 Spyder 的当前 iPython 控制台中执行,请选择“在当前控制台中执行”。

# -*- 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()

推荐阅读