Je suis un débutant dans la programmation Python et ayant des problèmes avec ce programme simple qui calcule le prix de carrelage données 2 dimensions:NameError: nom « coût » ne se définit pas - lorsque vous appelez la fonction
Objective: Calculate the total cost of tile it would take to cover a floor plan of width and height, using a cost entered by the user.
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)
I comprendre que je peux déclarer les variables en dehors de la fonction et le code fonctionnerait. Cependant, je veux ces variables à l'intérieur de la boucle afin qu'elles puissent être validées par except ValueError
.
Où définissez-vous le coût? Pourquoi dépensez-vous des frais si c'est ce que vous essayez de calculer? Lorsque vous écrivez 'cost_o_tile (cost, width, height)', vous donnez les données de la fonction. – Carcigenicate
@Carcigenicate: il a un coût par tuile (passé comme argument) et un coût pour l'ensemble du sol (ce qu'il calcule). –
@HughBothwell Ah – Carcigenicate