【问题标题】:Prompting Until a Match is Found [duplicate]提示直到找到匹配项 [重复]
【发布时间】:2017-10-24 19:38:28
【问题描述】:

您好,我正在研究如何计算立方体的体积

validInput 是 cube 或 c

validInput = ("cube" , "c")

这是如果要进行下一步应该输入的输入

inputShape = input("Please tell me the shape you want to use (For example, Cube) ").lower()   

while inputShape != validInput:
inputShape = input("You've entered an invalid input. Please try again ")

if inputShape in cube:
    cubeValue = float (input ("Please input the value you want to use to calculate the volume of the cube"))
    def cubeVolume(cubeValue):
        cubeVolumeValue = cubeValue ** 3
        return cubeVolumeValue
    print("The volume of a pyramid with a length ", cubeValue, "is" , cubeVolumeValue)

问题是,即使我输入 cube 或 c,我也会收到无效消息“您输入的输入无效。请重试”。

我该如何做才能继续下一步(输入浮点值)?

我卡住了帮助:(

【问题讨论】:

  • 嗨!感谢您添加代码。这看起来像 python,所以我添加了那个标签。
  • 您将validInput 定义为2 个字符串的元组,因此它从不 等于inputShape,这是一个字符串。此外,您需要缩进while 行之后的行,正确的缩进在 Python 中至关重要。为什么你在 if 块中定义了 cubeVolume 函数?

标签: python validation while-loop user-input


【解决方案1】:

看起来你搞砸了你的条件。应该是“当 inputShape 不在有效输入中时”。

现在您断言 inputShape 等于整个有效输入列表,但事实并非如此。您只需要检查字符串是否存在于列表中的某处。

【讨论】:

  • 另外,我不确定“if inputShape in cube”条件是什么。甚至不知道这是否会执行,因为立方体不是定义的变量。你想检查它是否是一个立方体吗?即使在这种情况下,您也已经通过 while 循环检查它是否是一个多维数据集,因此您不需要执行另一个断言。
  • 所以基本上,试图计算立方体、金字塔和椭圆体的体积。
  • 有什么方法可以通过私信联系你吗?
  • 我明白了,所以您要向 validInput 添加更多内容。要检查 inputShape 是否为立方体,您应该执行以下操作:if inputShape == "cube" or inputShape == "c"
  • Idk,我认为stackoverflow没有消息功能,但如果您有任何问题,请回复,我会尽快回复
猜你喜欢
  • 2020-01-30
  • 2016-04-28
  • 2017-04-08
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 2017-12-25
  • 1970-01-01
  • 2020-09-05
相关资源
最近更新 更多