[英]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:据我所知,这些问题似乎是我极其有限的:
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.