如果输入是Yes,为什么Python会突然停止?
我正试图用Python编写这个程序,它需要圆柱体的表面积和体积。最后,它询问用户是否要计算体积/表面积。但是,如果他们输入Yes,则不会发生任何事情。我的代码有什么问题如果输入是Yes,为什么Python会突然停止?,python,cylindrical,Python,Cylindrical,我正试图用Python编写这个程序,它需要圆柱体的表面积和体积。最后,它询问用户是否要计算体积/表面积。但是,如果他们输入Yes,则不会发生任何事情。我的代码有什么问题 第二,我尝试使用math.pi,但它不起作用,我该怎么办 代码很长,因此只能向下滚动到重要部分: print("Welcome to the volume and surface area cylinder calculator powered by Python!") response = input("To calculat
第二,我尝试使用math.pi,但它不起作用,我该怎么办 代码很长,因此只能向下滚动到重要部分:
print("Welcome to the volume and surface area cylinder calculator powered by Python!")
response = input("To calculate the volume type in 'vol', to calculate the surface area, type in 'SA': ")
if response=="vol" or response =="SA":
pass
else:
print("Please enter a correct statement.")
response = input("To calculate the volume type in 'vol', to calculate the surface area, type in 'SA': ")
if response=="vol":
#Below splits
radius, height = [float(part) for part in input("What is the radius and height of the cylinder? (e.g. 32, 15): ").split(',')]
PI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
volume = PI*radius*radius*height
decimal_places = int(input("How many decimal places do you want it to?: "))
print("The volume of the cylinder is {0:.{1}f}cm\u00b3".format(volume, decimal_places))
verify = input("Do you want to find out the surface area (type in Yes or No): ")
verify = verify.capitalize
if verify == "Yes":
radius, height = [float(part) for part in input("What is the radius and height of the cylinder? (e.g. 32, 15): ").split(',')]
PI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
SA = int(2)*PI*radius*radius+int(2)+radius*radius*height
decimal_places = int(input("How many decimal places do you want it to?: "))
print("The surface area of the cylinder is {0:.{1}f}cm\u00b2".format(SA, decimal_places))
if verify == "No":
pass
if response =="SA":
#Below splits
radius, height = [float(part) for part in input("What is the radius and height of the cylinder? (e.g. 32, 15): ").split(',')]
PI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
SA = int(2)*PI*radius*radius+int(2)+radius*radius*height
decimal_places = int(input("How many decimal places do you want it to?: "))
print("The surface area of the cylinder is {0:.{1}f}cm\u00b2".format(SA, decimal_places))
verify = input("Do you want to find out the volume (type in Yes or No): ")
verify = verify.capitalize
if verify == "Yes":
radius, height = [float(part) for part in input("What is the radius and height of the cylinder? (e.g. 32, 15): ").split(',')]
PI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286
volume = PI*radius*radius*height
decimal_places = int(input("How many decimal places do you want it to?: "))
print("The volume of the cylinder is {0:.{1}f}cm\u00b3".format(volume, decimal_places))
if verify == "No":
pass
您使用以下方法替换了
verify
:
这将永远不会匹配'Yes'
或'No'
,因为它不再是字符串。改为调用该方法:
verify = verify.capitalize()
请注意,您对的测试“No”
可以直接删除,测试一个字符串然后通过是没有意义的
使用math.pi
而不是pi
否则效果很好:
>>> import math
>>> math.pi
3.141592653589793
>>> radius, height = 32, 15
>>> 2 * math.pi * radius ** 2 + 2 * math.pi * radius * height
9449.910701998098
>>> math.pi * radius ** 2 * height
48254.86315913922
您使用以下方法替换了verify
:
这将永远不会匹配'Yes'
或'No'
,因为它不再是字符串。改为调用该方法:
verify = verify.capitalize()
请注意,您对的测试“No”
可以直接删除,测试一个字符串然后通过是没有意义的
使用math.pi
而不是pi
否则效果很好:
>>> import math
>>> math.pi
3.141592653589793
>>> radius, height = 32, 15
>>> 2 * math.pi * radius ** 2 + 2 * math.pi * radius * height
9449.910701998098
>>> math.pi * radius ** 2 * height
48254.86315913922
这是我修改过的版本。它避免了大量重复
from math import pi
print("Welcome to the volume and surface area cylinder calculator powered by Python!")
response = raw_input("To calculate the volume type in 'vol', to calculate the surface area, type in 'SA': ").lower()
while response not in ["vol", "sa"]:
print("Please enter a correct statement.")
response = raw_input("To calculate the volume type in 'vol', to calculate the surface area, type in 'SA': ").lower()
radius, height = [float(part) for part in raw_input("What is the radius and height of the cylinder? (e.g. 32, 15): ").split(',')]
r2 = radius ** 2
SA = 2 * pi * r2 + 2 + pi * radius * height
volume = pi * r2 * height
decimal_places = int(raw_input("How many decimal places do you want it to?: "))
if response=="vol":
print("The volume of the cylinder is {0:.{1}f}cm\u00b3".format(volume, decimal_places))
verify = raw_input("Do you want to find out the surface area (type in Yes or No): ")
if verify.lower() == "yes":
print("The surface area of the cylinder is {0:.{1}f}cm\u00b2".format(SA, decimal_places))
if response =="sa":
print("The surface area of the cylinder is {0:.{1}f}cm\u00b2".format(SA, decimal_places))
verify = raw_input("Do you want to find out the volume (type in Yes or No): ")
if verify.lower() == "yes":
print("The volume of the cylinder is {0:.{1}f}cm\u00b3".format(volume, decimal_places))
这是我修改过的版本。它避免了大量重复
from math import pi
print("Welcome to the volume and surface area cylinder calculator powered by Python!")
response = raw_input("To calculate the volume type in 'vol', to calculate the surface area, type in 'SA': ").lower()
while response not in ["vol", "sa"]:
print("Please enter a correct statement.")
response = raw_input("To calculate the volume type in 'vol', to calculate the surface area, type in 'SA': ").lower()
radius, height = [float(part) for part in raw_input("What is the radius and height of the cylinder? (e.g. 32, 15): ").split(',')]
r2 = radius ** 2
SA = 2 * pi * r2 + 2 + pi * radius * height
volume = pi * r2 * height
decimal_places = int(raw_input("How many decimal places do you want it to?: "))
if response=="vol":
print("The volume of the cylinder is {0:.{1}f}cm\u00b3".format(volume, decimal_places))
verify = raw_input("Do you want to find out the surface area (type in Yes or No): ")
if verify.lower() == "yes":
print("The surface area of the cylinder is {0:.{1}f}cm\u00b2".format(SA, decimal_places))
if response =="sa":
print("The surface area of the cylinder is {0:.{1}f}cm\u00b2".format(SA, decimal_places))
verify = raw_input("Do you want to find out the volume (type in Yes or No): ")
if verify.lower() == "yes":
print("The volume of the cylinder is {0:.{1}f}cm\u00b3".format(volume, decimal_places))
出于好奇,我不知道python.in python是“yes”=“yes”?如何用math.pi替换所有数字?其次,如果verify==“No”,如何使其退出?如果我尝试导入sys.exit(),它将不起作用?谢谢again@KaushikSivakumar当前位置不是。字符串区分大小写。@Timmy:当选择了'No'
时,您的程序已经自然结束了,为什么您需要显式地这样做?@Timmy:sys.exit()
不起作用,因为str.capitale='No'
永远不会是真的。只是出于好奇,我不知道python中的是“yes”=“yes”?如何用math.pi替换所有数字?其次,如果verify==“No”,如何使其退出?如果我尝试导入sys.exit(),它将不起作用?谢谢again@KaushikSivakumar当前位置不是。字符串区分大小写匹配。@Timmy:当选择了'No'
时,您的程序已经自然结束了,为什么您需要显式地这样做?@Timmy:sys.exit()
不起作用,因为str.capitale=='No'
永远不会是真的。“我尝试使用math.pi,但它不起作用。”。在寻求帮助时,你需要更加具体。还有,神圣的复制和粘贴,蝙蝠侠!即使math.pi
不起作用,为什么您认为需要重新定义pi
4次?“我尝试使用math.pi,但它不起作用”。在寻求帮助时,你需要更加具体。还有,神圣的复制和粘贴,蝙蝠侠!即使<代码>数学> PI/<代码>不起作用,为什么你认为你需要重新定义<代码> PI/<代码> 4次?注意:Ravi in PUTE()在Python 3.XI中已经被更改为输入。个人认为这个答案是最好的,因为我可以直接与我的比较,看看我能改进什么。伟大的作品格雷姆·斯图尔特!注意:RyxInPUTE()已经在Python 3.XI中更改为输入,个人认为这个答案是最好的,因为我可以直接与我的比较,看看我可以改进什么。伟大的作品格雷姆·斯图尔特!