cheese = float(input("Enter cheese order weight (numeric value): "))
if cheese >= 100:
print(cheese,"is more than currently available stock ")
elif cheese <= 0.15:
print(cheese,"is below minimum order amount")
elif cheese >= 0.16 <= 99:
print(cheese," costs $",cheese * 7.99,sep = "")
else:
print(cheese,"Is invlide, please try agin")每次我添加一个字母时,我都会得到一个错误。我希望能够添加float nub和单词,这样我就可以使用else语句。
发布于 2020-04-20 08:52:31
我不知道“矮子”是什么意思?但您需要将其放入循环中,并使用try和except处理错误
cheese_weight = None
# Repeat until there is a valid value for cheese_weight
while not cheese_weight:
# Step 1: get some user input
raw_input = input("Enter cheese order weight (numeric value): ")
# Step 2: try convert this input to a float
try:
cheese_weight = float(raw_input)
# Step 3: handle the error if there is one
except ValueError as err:
print("The cheese weight must be a numeric value.)请注意,如果float(raw_input)引发错误,cheese_weight将不会获得新值,因此循环将再次运行。如果值解析为浮点型,那么cheese_weight将有一个不是None的值,因此循环将结束。
https://stackoverflow.com/questions/61313384
复制相似问题