【问题标题】:Why Spyder will not run or debug my code?为什么 Spyder 不会运行或调试我的代码?
【发布时间】:2020-04-11 04:22:24
【问题描述】:

我是在 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()

【问题讨论】:

  • 我正在尝试运行它,以便查看我得到的错误你不知道错误吗?
  • 编辑:我在缩进中不再使用不一致的制表符,但现在我收到“语法错误:无法分配给比较”。对于说ace in cardlist = 11的行
  • 那在哪里?你希望这个表达式做什么?
  • 它在第 126 行和第 220 行,我希望它只使卡片列表中的第一个 ace 等于 11,而其他 ace 等于 1。
  • 那段代码太密集了,我读不下去了。

标签: python python-3.x debugging spyder ipdb


【解决方案1】:

关于:

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

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

根据Spyder Python indentation的回答 试试

在 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()

【讨论】:

  • break 中的刹车点?当我按下制表符时,它会在第 69 行缩进两次。一个缩进是制表符缩进,另一个是四个空格。
  • 整个文件中的所有缩进必须是相同的类型。抽头或缩进不混合。请看一下链接。断点与 E.G. Loops 中的刹车无关
  • 断点允许程序员在程序到达特定行时“暂停”程序。然后,它允许您检查代码中变量的值。这在调试程序中的逻辑错误时非常有用 - 但是您需要先修复语法。此外,由于您在问题中谈到的错误 - 如果您确保不在同一个文件中混合空格和制表符,该错误将会消失。在任何地方使用其中一种。查找和替换将对您有很大帮助!
  • 我在代码末尾添加了一个断点,但它仍然无法启动。而且,我不能在查找和替换中用制表符替换空格,也不能相反。
  • @BigBird 我已经编辑了答案以显示如何替换空格并添加了指向 python 调试教程的链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
  • 2018-09-06
相关资源
最近更新 更多