我正在尝试在您输入 RecPyrSA 时获取它,它将播放 def RecPyrSA,但我不确定如何实现。我也是 python 新手,不知道该怎么做,所以我只想制作一个有趣的脚本。
from math import e
def RecPyrSA():
recpyrl=float(input("Base Length "))
recpyrw=float(input("Base Width "))
recpyrthl=float(input("Triangle Height On The Legth Side "))
recpyrthw=float(input("Triangle Height On The Width Side "))
recpyrsa=(recpyrl*recpyrw+recpyrthl*recpyrl+recpyrthw*recpyrw)
print(recpyrsa)
def ConeSA():
print ("type r then l")
x=float(input("r "))
y=float(input("l "))
csa=(3.14*x*x)+(3.14*y*x)
print(csa)
start=input("Type Here --> ")
while True:
print ("For the surface area of a rectangular pyramid type RecPyrSA")
SAstart=input("Type here ---> ")
if SAstart == "RecPyrSA"
RecPyrSA
break
else:
print ("Incorrect Code")
print ("Try Again")
答案 0 :(得分:0)
您没有调用该函数。
RecPyrSA
只是一个带有函数对象的变量。一旦你引用了一个函数对象,它就会用括号调用
RecPyrSA()
答案 1 :(得分:0)
您需要调用函数,该函数使用括号 function_name()
。此外,您似乎没有使用 e
中的 math
,因此您不需要导入它。您还缺少行尾的冒号:if SAstart == "RecPyrSA"
。根据 PEP 8
,您还应该在运算符的任一侧添加空格。同样根据 PEP 8
,函数名称应该在 snake_case
中。
有变化的代码:
def rec_pyr_SA():
recpyrl = float(input("Base Length "))
recpyrw = float(input("Base Width "))
recpyrthl = float(input("Triangle Height On The Legth Side "))
recpyrthw = float(input("Triangle Height On The Width Side "))
recpyrsa = recpyrl * recpyrw + recpyrthl * recpyrl + recpyrthw * recpyrw
print(recpyrsa)
def cone_sa():
print("type r then l")
x = float(input("r "))
y = float(input("l "))
csa = (3.14 * x * x) + (3.14 * y * x)
print(csa)
while True:
print ("For the surface area of a rectangular pyramid type RecPyrSA")
SA_start = input("Type here ---> ")
if SA_start == "RecPyrSA":
rec_pyr_SA()
break
else:
print("Incorrect Code")
print("Try Again")