我的elif行不断出现语法错误(箭头指向“ elif”的结尾。
我正在尝试编写一个程序,该程序允许用户输入3D形状,然后输入一些数据来计算其体积。但是,我不断收到语法错误,箭头指向我的“ elif”结尾。
我正在使用https://www.onlinegdb.com/online_python_interpreter作为控制台。
# Allows the use of Pi
import math
# Where the user selects the 3D shape
shape = str(input("Enter a Shape you would like to find the volume of! : "))
# Where it takes the user to a shape calculator based on what shape the user inputted
if shape == ("Sphere"):
def vsphere(r):
volume = 4.0/3.0*math.pi*r**3
return volume
# Where the user enters the data of the shape
radius = float(input("Enter The Volume of Your Sphere! : "))
print("The Volume of a Sphere with a Radius of ",str(radius)," is ",str(vsphere(radius)))
elif shape == ("Cylinder"):
def vcylinder(n):
volume = math.pi * r**2 * h
return n
r = int(input("Enter the Radius! : "))
h = int(input("Enter the Height! : "))
print("The Volume of a Cylinder with a Radius of ",str(r)," and a Height of ",str(h)," is ",str(vcylinder(math.pi * r**2 * h)))
elif shape == ("Cone"):
def vcone(n):
volume = math.pi * r**2 * h * 1/3
return n
r = int(input("Enter the Radius! : "))
h = int(input("Enter the Height! : "))
print("The Volume of a Cylinder with a Radius of ",str(r)," and a Height of ",str(h)," is ",str(vcone(math.pi * r**2 * h * 1/3)))
elif shape == ("Cube"):
def vcube(a):
volume = a**3
return volume
a = int(input("Enter the Area! : "))
print("The Volume of a Cube with an Area of ",str(a)," is ",str(vcube(a)))
else:
print("Sorry, that Shape isn't on our List")
print("Sorry, that Shape isn't on our List")
它应该允许用户输入一个形状(如立方体),将其带到形状计算器(输入立方体的面积),然后根据输入的数据计算体积。
相反,当我运行程序时,它会显示以下错误消息:
文件“ main.py”,第27行
elif shape ==(“圆锥”):
^
SyntaxError:语法无效
答案 0 :(得分:1)
您遇到缩进错误问题。
您必须缩进
r = int(input("Enter the Radius! : "))
h = int(input("Enter the Height! : "))
如果我们不缩进,则视为退出if
块。因此,当我们在elif
之外遇到if
时,会得到语法错误。
# Allows the use of Pi
import math
# Where the user selects the 3D shape
shape = str(input("Enter a Shape you would like to find the volume of! : "))
# Where it takes the user to a shape calculator based on what shape the user inputted
if shape == ("Sphere"):
def vsphere(r):
volume = 4.0/3.0*math.pi*r**3
return volume
# Where the user enters the data of the shape
radius = float(input("Enter The Volume of Your Sphere! : "))
print("The Volume of a Sphere with a Radius of ",str(radius)," is ",str(vsphere(radius)))
elif shape == ("Cylinder"):
def vcylinder(n):
volume = math.pi * r**2 * h
return n
r = int(input("Enter the Radius! : "))
h = int(input("Enter the Height! : "))
print("The Volume of a Cylinder with a Radius of ",str(r)," and a Height of ",str(h)," is ",str(vcylinder(math.pi * r**2 * h)))
elif shape == ("Cone"):
def vcone(n):
volume = math.pi * r**2 * h * 1/3
return n
r = int(input("Enter the Radius! : "))
h = int(input("Enter the Height! : "))
print("The Volume of a Cylinder with a Radius of ",str(r)," and a Height of ",str(h)," is ",str(vcone(math.pi * r**2 * h * 1/3)))
elif shape == ("Cube"):
def vcube(a):
volume = a**3
return volume
a = int(input("Enter the Area! : "))
print("The Volume of a Cube with an Area of ",str(a)," is ",str(vcube(a)))
else:
print("Sorry, that Shape isn't on our List")
print("Sorry, that Shape isn't on our List")
答案 1 :(得分:1)
您正在缩进。
您的
elif
的缩进级别必须相同
。 Cylinder
的计算必须在elif shape == ("Cylinder"):
中。
elif shape == ("Cylinder"):
def vcylinder(n):
volume = math.pi * r**2 * h
return n
# vvvvvvvvvvvvvvvvvv NEED INDENTATION vvvvvvvvvvvvvvvvvv
r = int(input("Enter the Radius! : "))
h = int(input("Enter the Height! : "))
print("The Volume of a Cylinder with a Radius of ",str(r)," and a Height of ",str(h)," is ",str(vcylinder(math.pi * r**2 * h)))
# ^^^^^^^^^^^^^^^^^^ NEED INDENTATION ^^^^^^^^^^^^^^^^^^
elif shape == ("Cone"):
def vcone(n):
volume = math.pi * r**2 * h * 1/3
return n
ps:比较2个字符串时无需使用括号