[英]I don't under the reason for this error in the print_output function
I don't under the reason for this error in the print_output function.我不知道 print_output 函数中出现此错误的原因。 This may be an issue with the other functions.
这可能是其他功能的问题。 I am not sure what the formatting error is.
我不确定格式错误是什么。
''' '''
def print_output(i):
print (f"OUTPUT {i}")
def triangle_hypotenuse(side_1, side_2):
sqrt((side_1 ** 2) + (side_2 ** 2)) = h
h = "{:.2f}".format(h)
print_output(h)
def feet_to_meters(feet):
meters = feet/3.281
meters = "{:.4f}".format(meters)
print_output(meters)
def polar_coords(x,y):
radius = math.sqrt( x * x + y * y )
theta = math.atan(y/x)
theta = 180 * theta/math.pi
r = "{:.2f}".format(radius)
t = "{:.2f}".format(theta)
phrase_1 = "r: " + r
phrase_2 = "theta: " + t
combined = phrase_1 + phrase_2
print_output(combined)
def dollars_to_euros(dollars):
euros = dollars * 0.99
euros = "{:.2f}".format(dollars)
print_output(euros
Error :
>>> print_output("Hello World")
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
print_output("Hello World")
NameError: name 'print_output' is not defined
''' '''
Create a txt file and name it to "myfile.py".创建一个 txt 文件并将其命名为“myfile.py”。 Make sure your file extension has been changed from .txt to .py.
确保您的文件扩展名已从 .txt 更改为 .py。 Then paste the following in that python file which you can open with either an IDE or notepad.
然后将以下内容粘贴到该 python 文件中,您可以使用 IDE 或记事本打开该文件。
import math
def print_output(i):
print (f"OUTPUT {i}")
def triangle_hypotenuse(side_1, side_2):
h = math.sqrt((side_1 ** 2) + (side_2 ** 2))
h = "{:.2f}".format(h)
print_output(h)
def feet_to_meters(feet):
meters = feet/3.281
meters = "{:.4f}".format(meters)
print_output(meters)
def polar_coords(x,y):
radius = math.sqrt( x * x + y * y )
theta = math.atan(y/x)
theta = 180 * theta/math.pi
r = "{:.2f}".format(radius)
t = "{:.2f}".format(theta)
phrase_1 = "r: " + r
phrase_2 = "theta: " + t
combined = phrase_1 + phrase_2
print_output(combined)
def dollars_to_euros(dollars):
euros = dollars * 0.99
euros = "{:.2f}".format(dollars)
print_output(euros)
if __name__ == "__main__":
print_output("Hello World")
Now in the address bar where the python file exists, type CMD and press enter as shown in this screenshot现在在 python 文件所在的地址栏中,键入 CMD 并按 Enter 键,如此屏幕截图所示
In the command terminal, type python myfile.py
and your python file will execute在命令终端中,键入
python myfile.py
,您的 python 文件将执行
According to my VSCode's Python Extension, there are multiple issues in this code.根据我的 VSCode 的 Python 扩展,这段代码存在多个问题。 I tried to correct the errors:
我试图纠正错误:
import math
def print_output(i):
print (f"OUTPUT {i}")
def triangle_hypotenuse(side_1, side_2):
h = math.sqrt((side_1 ** 2) + (side_2 ** 2))
h = "{:.2f}".format(h)
print_output(h)
def feet_to_meters(feet):
meters = feet/3.281
meters = "{:.4f}".format(meters)
print_output(meters)
def polar_coords(x,y):
radius = math.sqrt( x * x + y * y )
theta = math.atan(y/x)
theta = 180 * theta/math.pi
r = "{:.2f}".format(radius)
t = "{:.2f}".format(theta)
phrase_1 = "r: " + r
phrase_2 = "theta: " + t
combined = phrase_1 + phrase_2
print_output(combined)
def dollars_to_euros(dollars):
euros = dollars * 0.99
euros = "{:.2f}".format(dollars)
print_output(euros)
print_output("Hello World")
Corrections:更正:
sqrt((side_1 ** 2) + (side_2 ** 2)) = h
is invalid.sqrt((side_1 ** 2) + (side_2 ** 2)) = h
无效。)
missing at the end of the function dollars_to_euros
.dollars_to_euros
末尾缺少)
。To run your function the computer needs to know where to look for it.要运行您的功能,计算机需要知道在哪里寻找它。 If you run the functions straight in the command line will not work unless you have written the function there too.
如果你直接在命令行中运行这些函数,除非你也在那里编写了函数,否则它们将不起作用。
You have probably saved your code in a file, for example test.py.您可能已将代码保存在文件中,例如 test.py。 Add this at the top or at the bottom of the code.
将此添加到代码的顶部或底部。 And run the file either in your interpreter (vscode, intellij, pycharm, python's own or whatever).
并在您的解释器(vscode、intellij、pycharm、python 自己的或其他)中运行该文件。
def print_output(i):
print (f"OUTPUT {i}")
.
.
.
print_output("hello")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.