[英]How to fix "Syntax Error, Invalid Syntax" (No context or reason why) Elif Function Python
我的 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 形状 ==(“锥体”):
^
语法错误:无效语法
您遇到了缩进错误问题。
你必须缩进
r = int(input("Enter the Radius! : "))
h = int(input("Enter the Height! : "))
如果我们不缩进它们,则视为您已退出if
块。 因此,当我们在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")
你在搞缩进。
您的
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个字符串时不需要使用括号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.