Python-Debug(pycharm中调试细节)

pycharm常用debug按钮:

Show Execution Point(Alt + F10):跳转到代码执行位置

Step Over(F8):执行当前代码,但是不会进入函数

Step into(F7):执行当前代码,会进入函数(包括源码)

Step Into my Code(Alt + shift + F7):执行当前代码,会进入函数(仅包括自己写的代码)

Step out(shift + F8):跳出函数体

例子:

玩家输入剪刀石头布与机器人PK,自动输出结果

两个函数,mian执行函数,computer电脑函数,源码如下:

main.py

from computer import Robot,Calculate

if __name__ == '__main__':
    person_put = int(input("请输入1=剪刀, 2=石头, 3=布\n"))
    robot = Robot()
    robot_put = robot.run()
    calculate = Calculate(person_put,robot_put)
    result = calculate.result()
    print("robot is %d, result is %s" %(robot_put,result))

computer.py

import random

class Robot(object):
    def __init__(self):
        self.robot_putlist = [1,2,3] #1=剪刀,2=石头,3=布

    def run(self):
        return random.choice(self.robot_putlist)

class Calculate(object):
    def __init__(self,person_put,robot_put):
        self.person_put = person_put
        self.robot_put = robot_put

    def result(self):
        if (self.person_put == 1 and self.robot_put == 1) or (
                self.person_put == 2 and self.robot_put == 2) or (
                self.person_put == 3 and self.robot_put == 3):
            return "draw"
        elif (self.person_put == 1 and self.robot_put == 2) or (
                self.person_put == 2 and self.robot_put == 3) or (
                self.person_put == 3 and self.robot_put == 1):
            return "lose"
        elif (self.person_put == 1 and self.robot_put == 3) or (
                self.person_put == 2 and self.robot_put == 1) or (
                self.person_put == 3 and self.robot_put == 2):
            return "win"
        else:
            return "null"

debug步骤:

1.debug前打断点,点击代码左侧,生成红点

2. debug代码,右键main.py空白处,或者if __name__ == "main"旁边的小三角形

3. debug后,代码会执行到断点处,因为前一句是input,所有需要先在控制台输入

 此时断点行显示蓝色,蓝色代表未执行,处于当前位置

4.1 这时可以进行调试,如F7,则执行此代码,并进入函数

 4.2 也可以使用F7,不进入函数,直接获取返回结果,到下一句代码处

5. 依次执行后,也可以在控制台variables查看变量的变化

posted @ 2022-02-09 16:45  SiNanhong  阅读(785)  评论(0编辑  收藏  举报