【发布时间】: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