简体   繁体   English

我不在 print_output 函数中出现此错误的原因

[英]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:更正:

  1. I am not sure if it is due to copy-paste, but the indication is wrong for the time being.我不确定这是否是由于复制粘贴造成的,但目前指示是错误的。 The first space character before each line had to be removed.必须删除每行之前的第一个空格字符。
  2. The expression sqrt((side_1 ** 2) + (side_2 ** 2)) = h is invalid.表达式sqrt((side_1 ** 2) + (side_2 ** 2)) = h无效。
  3. There is a ) 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.

相关问题 不要打印函数的结果 - don't print results of a function 使用范围时我没有得到函数的打印结果 - I don't get a print result of function when using range 我的函数中的打印语句出现语法错误。 我不知道我做错了什么 - I get syntax error on my print statement in my function. I don't know what I am doing wrong 为什么我没有得到想要的 output? 在这个递归的 function 中? - Why don't I get the desired output? In this recursive function? 打印输出它不显示任何内容 - print the output It don`t show anything 是什么原因导致我在打印args时没有得到所有元组中的元素。 在这里args [0]打印2而不是1。 - What is the reason i don't get all the elements in the tuple when i print args. Here args[0] prints 2 instead of 1. 如何摆脱这个索引超出范围错误? 知道原因但不知道解决办法 - How to get rid of this index out of range error? I know the reason but don't know the solution 输入功能不显示,打印时为“空白” - Input function don´t show and is "blank" in print 程序从字典中打印出正确的 output,但我不完全理解打印语句是如何工作的 - Program prints correct output from dictionary, but I don't fully understand how the print statements work 我找到了错误的原因,但我不知道如何解决 - I found the reason for the mistake but I don't know how to solve it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM