python程序编写中需要注意的一些细节

1.运行python,里面有一个参数文件text.txt,运行就报FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'错误,查找原因,很多人都说是路径问题,

后面查找很久,发现是电脑自动隐藏文件类型,所以简称的test文件就变成test.txt.txt,所以查找不到文件

 

2.在使用def时,前面不能有空格,否则函数会报错,如

 

#定义加函数
def add(a,b):
print(f"ADDING {a} + {b}")
return a + b

 

#定义减函数
def subtract(a,b):
print(f"SUBTRACTING {a} - {b}")
return a - b

 

#定义乘函数
def multiply(a,b):
print(f"MULTTPLYING {a} * {b}")
return a * b

 

#定义除函数
  def divide(a,b):
print(f"DIVIDING {a} / {b}")
return a / b

 

print("Let's do some math with functions!")

 

age = add(30 ,5)
height = subtract( 78, 4)
weight = multiply(90,2)
iq = divide(100 ,2)

 

print(f"Age: {age},Height: {height},Weight: {weight},IQ: {iq}")

 

# A puzzle for the extra credit,type it in anyway.
print("Here is a puzzle ")

 

what = add(age ,subtract(height,multiply(weight,divide (iq,2))))

 

print("That become :",what ,"Can you do it by hand")

运行就会报

PS C:\Users\hero\lpthw> python ex21.py
File "C:\Users\hero\lpthw\ex21.py", line 12
def multiply(a,b):

检查发现divide函数前面有空格,报警

 

posted @ 2020-12-11 16:50  herohaung  阅读(318)  评论(0)    收藏  举报