【问题标题】:Why does Python suddenly stop if the input is Yes?如果输入是 Yes,为什么 Python 会突然停止?
【发布时间】:2013-10-03 21:33:03
【问题描述】:

我正在尝试用 Python 编写这个程序,它会询问圆柱体的表面积和体积。最后,它会询问用户是否要计算体积/表面积。但是,如果他们确实输入 Yes,则不会发生任何事情。我的代码有什么问题?

其次,我尝试使用 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

【问题讨论】:

  • “我尝试使用 math.pi 但它没有用”。在寻求帮助时,您需要更加具体。
  • 还有,神圣的复制粘贴,蝙蝠侠!即使math.pi 不起作用,您为什么认为需要重新定义PI 4 次?

标签: python cylindrical


【解决方案1】:

您将verify 替换为方法

verify = verify.capitalize

这永远不会匹配'Yes''No',因为它不再是一个字符串。改为调用该方法:

verify = verify.capitalize()

请注意,您对"No" 的测试可以被删除,测试字符串没有什么意义,然后只测试passing。

使用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

【讨论】:

  • 只是出于好奇,我不知道 python.in python 是 "yes" = "YES" 吗?
  • 如何用 math.pi 替换所有数字?其次,如果验证==“否”,我怎样才能让它退出?如果我尝试 import sys sys.exit() 它不会工作?再次感谢
  • @KaushikSivakumar:不是。字符串区分大小写。
  • @TimTimmy:当'No' 被选中时,你的程序已经自然结束了,你为什么需要明确地这样做?
  • @TimTimmy:sys.exit() 不起作用,因为 str.capitalize == 'No' 永远不会是 True。
【解决方案2】:

这是我的调整版本。它避免了很多重复。

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))

【讨论】:

  • 注意:raw_input() 在 Python 3.x 中已更改为输入
  • 我个人认为这个答案是最好的,因为我可以直接与我的答案进行比较,看看我可以改进什么。伟大的工作格雷姆斯图尔特!
猜你喜欢
  • 2010-10-31
  • 1970-01-01
  • 2019-11-23
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 2019-02-26
  • 2019-10-15
  • 1970-01-01
相关资源
最近更新 更多