简体   繁体   English

MS Learn Python - 代码未在 Visual Studio 中运行(新手)

[英]MS Learn Python - code not running in Visual Studio (newbie)

I am new to programming and have started the Microsoft first steps with Python course.我是编程新手,并且已经通过 Python 课程开始了 Microsoft 的第一步。 I have navigated the installation of Python (3.9), Visual Studio Code, the Python extension for Visual Studio and have started into some of the practice exercises.我已经完成了 Python (3.9)、Visual Studio Code、Python 扩展的安装,并开始了一些练习。

I have tried to run some of the practice programs but am not having much success.我曾尝试运行一些练习程序,但没有取得多大成功。 To try and identify the source of the problem, I have copied the provided Microsoft code from one of the examples.为了尝试找出问题的根源,我从其中一个示例中复制了提供的 Microsoft 代码。 The issues appear to be as far as I can tell from my extremely limited understanding:据我所知,这些问题似乎是我极其有限的:

  1. The program is not running/partially running;程序未运行/部分运行; and
  2. The program is returning the following error: ValueError: invalid literal for int() with base 10:程序返回以下错误: ValueError: invalid literal for int() with base 10:

My assumption is that this might have something to do with Visual Studio set up as the output from the Microsoft example code does not match the provided output.我的假设是,这可能与 Visual Studio 设置有关,因为 Microsoft 示例代码中的 output 与提供的 output 不匹配。

The source code is源代码是

print("Today's date?")
date = input()
print("Breakfast calories?")
breakfast = int(input())
print("Lunch calories?")
lunch = int(input())
print("Dinner calories?")
dinner = int(input())
print("Snack calories?")
snack = int(input())
total = breakfast + lunch + dinner + snack
print("Calorie content for " + date + ": " + str(total))

The terminal output I am getting after running the program three times is:我运行程序三次后得到的终端output是:

PS C:\Users\domch\OneDrive\5_CPD\Python learn\Hello> & C:/Users/domch/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/domch/OneDrive/5_CPD/Python learn/Calorie counter/calories.py"
Today's date?
& C:/Users/domch/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/domch/OneDrive/5_CPD/Python learn/Calorie counter/calories.py"
Breakfast calories?
& C:/Users/domch/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/domch/OneDrive/5_CPD/Python learn/Calorie counter/calories.py"
Traceback (most recent call last):
  File "c:\Users\domch\OneDrive\5_CPD\Python learn\Calorie counter\calories.py", line 4, in <module>
    breakfast = int(input())
ValueError: invalid literal for int() with base 10: '& C:/Users/domch/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/domch/OneDrive/5_CPD/Python learn/Calorie counter/calories.py"'
PS C:\Users\domch\OneDrive\5_CPD\Python learn\Hello> 

I understand the 'solution' to this example code is supposed to look like this in the terminal:我了解此示例代码的“解决方案”在终端中应该如下所示:

Today's date?
December 7th, 2020
Breakfast calories?
250
Lunch calories?
300
Dinner calories?
500
Snack calories?
150
Calorie content for December 7th, 2020: 1200

Any help would be greatly appreciated!!任何帮助将不胜感激!!

You are not running the program 3 times but actually attempting to run it when the previous run is still expecting your input (look at what the version read as input (it is in the error message from ValueError exception:您没有运行该程序 3 次,但实际上在前一次运行仍期待您的输入时尝试运行它(查看作为输入读取的版本(它位于来自ValueError异常的错误消息中:

'& C:/Users/domch/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/domch/OneDrive/5_CPD/Python learn/Calorie counter/calories.py"' '& C:/Users/domch/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/domch/OneDrive/5_CPD/Python learn/卡路里计数器/calories.py"'

. . Look for a terminal tab within VS Code and enter the input there on the first run.在 VS Code 中查找终端选项卡,并在第一次运行时在此处输入输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Visual Studio Code中运行Python代码? - Running Python code inside Visual Studio Code? 在 Visual Studio 代码中运行 python 代码? - Running python code in Visual Studio code? 错误:在MS Visual Studio 2015中使用Python运行C ++代码时出现“ python35_d.dll丢失” - Error: 'python35_d.dll is missing' while running C++ code with Python in MS Visual Studio 2015 Visual Studio Code 终端在 Powershell 中继续运行 Python 脚本 - Visual Studio Code Terminal keeps running Python script in Powershell 在 Visual Studio Code 中运行 python 脚本; 如何让 `input ()` 工作? - Running python script in Visual Studio Code; how to get `input ()` to work? 在Visual Studio中运行Python代码 - 安装pip无法正常工作 - Running Python code in Visual Studio - installing pip not working 在visual studio代码中同时运行两个python文件 - Running two python file at the same time in visual studio code 在 Visual Studio Code 中运行 Anaconda python Hello World 的语法无效 - invalid syntax running Anaconda python Hello World in Visual Studio Code Python计划脚本在IDLE中运行,但不在Visual Studio Code中运行 - Python schedule script running in IDLE but not in Visual Studio Code Visual Studio Code 可以在运行或调试 Python 的过程中启动 xinit 吗? - Can Visual Studio Code launch xinit as part of running or debugging Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM