我是Python编程的新手,并且在这个简单的程序中遇到了麻烦,该程序计算了2维的平铺价格:
目标:使用用户输入的成本计算覆盖宽度和高度平面图所需的平铺总成本。
print ("NOTE: The unit of cost is in dollars and dimension unit is in feet")
def cost_o_tile(cost, width, height):
while True:
cost = int(input("Cost of each tile:"))
width = int(input("What is the width of the floor?"))
height = int(input("What is the height of the floor?"))
try:
if cost < 0 or width < 0 or height <0:
print ("\n Please enter non-negative integers")
break
else:
return ("In order to cover your {} X {} floor, you will need to pay {} dollars".format(width,height,cost*width*height))
except ValueError:
print ("No valid integer! Please try again ...")
cost_o_tile(cost, width, height)
我知道我可以在函数之外声明变量,代码也可以。但是,我想在循环中使用这些变量,以便except ValueError
验证它们。
答案 0 :(得分:1)
好吧,你的函数cost_o_lite
不应该带任何参数:
def cost_o_tile():
...
print(cost_o_tile())
不要忘记打印结果。
您也可以分开关注:
首先写一个算法来计算总费用:
def cost_o_tile(cost, width, height):
if cost < 0 or width < 0 or height < 0:
raise ValueError
return cost * width * height
然后编写用户界面代码:
print ("NOTE: The unit of cost is in dollars and dimension unit is in feet")
while True:
try:
cost = int(input("Cost of each tile:"))
width = int(input("What is the width of the floor?"))
height = int(input("What is the height of the floor?"))
total = cost_o_tile(cost, width, height)
print("In order to cover your {} X {} floor, you will need to pay {} dollars"
.format(width, height, total))
break
except ValueError:
print ("No valid integer! Please try again ...")
答案 1 :(得分:1)
使您的函数尽可能纯粹是编写良好,可维护代码的关键,这当然不包括潜在的无限循环和用户输入。
全局范围内没有变量cost
,width
或height
。这是导致错误的原因。
首次通过
def cost_o_tile(cost, width, height):
return ("In order to cover your {} X {} floor, you will need to pay {} dollars"\
.format(width, height, cost * width * height))
cost, width, height = map(int, input("Enter 3 space separated integers: ").split())
print(cost_o_tile(cost, width, height))
第二次通过
一旦基本程序正常工作,您可以查看错误处理:
def cost_o_tile(cost, width, height):
try:
if cost < 0 or width < 0 or height < 0:
return "Parameters cannot be lesser than 0"
except ValueError:
return "Please provide numbers only"
return ("In order to cover your {} X {} floor, you will need to pay {} dollars"\
.format(width, height, cost * width * height))
cost, width, height = map(int, input("Enter 3 space separated integers: ").split())
print(cost_o_tile(cost, width, height))
最终通行证
现在,在处理错误后,您最终可以查看循环。
def cost_o_tile(cost, width, height):
try:
if cost < 0 or width < 0 or height < 0:
return "Parameters cannot be lesser than 0"
except ValueError:
return "Please provide numbers only"
return ("In order to cover your {} X {} floor, you will need to pay {} dollars"\
.format(width, height, cost * width * height))
if __name__ == '__main__':
while True:
cost, width, height = map(int, input("Enter 3 space separated integers: ").split())
print(cost_o_tile(cost, width, height))
if input("Continue? ").lower() not in {'y', 'ye', 'yes'}:
break
答案 2 :(得分:0)
cost_o_tile(cost, width, height)
当您在代码的第18行调用cost
函数时,问题出在cost_o_tile
参数上。如果你仔细观察,它还没有在函数范围之外定义,因此错误。