Visual Studio Code - Python 中的输入函数 [英] Visual Studio Code - input function in Python

查看:58
本文介绍了Visual Studio Code - Python 中的输入函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 Visual Studio Code,以学习 Python.

I am trying out Visual Studio Code, to learn Python.

我正在编写一段起始代码,以接收用户的输入,例如:

I am writing a starter piece of code to just take an input from the user, say:

S = input("What's your name? ")

当我尝试运行它时(Mac:Cmd + Shift + B)我看到任务正在运行,没有输出.我已经为输出和参数配置了 tasks.json 文件.

When I try to run this (Mac: Cmd + Shift + B) I see the task is running with no output. I have already configured the tasks.json file for output and arguments.

print("Hello, World!")
S = input("What's your name? ")

是否需要在 Visual Studio Code 中配置一些环境变量?

Do I need to configure some environment variables in Visual Studio Code?

推荐答案

任务旨在构建您的应用程序.由于 Python 是解释型的,因此您根本不需要使用 tasks.json 来运行/调试您的 Python 代码.改用launch.json.我正在使用 Don Jayamanne 的 Python 扩展进行调试,并将 launch.json 配置如下:

Tasks are intended for building your application. Since Python is interpreted, you don't need to use tasks.json at all to run/debug your Python code. Use the launch.json instead. I am using Don Jayamanne's Python extension for debugging and have configured the launch.json as follows:

  1. 打开命令面板 (Ctrl + Shift + P) 并写入命令:

  1. Open the Command Palette (Ctrl + Shift + P) and write the command:

无需调试即可启动

  • 然后选择您的环境 -> 单击 Python.这应该会在当前目录的 .vscode 目录中创建一个 launch.json 文件.

  • Then select your Environment -> Click Python. This should create a launch.json file within a .vscode directory in the current directory.

    粘贴以下配置json

    {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": true,
            "pythonPath": "${config.python.pythonPath}",
            "program": "${file}",
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ],
            "console": "integratedTerminal"
        }
    ]}
    

  • 保存文件,在编辑器中打开你的 python 脚本,然后再次开始而不调试".这应该会启动一个集成终端,您可以在其中提供输入和查看输出.

  • Save the file, open your python script in the editor and 'start without debugging' again. This should launch an integrated terminal where you can give input as well as see the output.

    这篇关于Visual Studio Code - Python 中的输入函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆