-4
I’m starting now and I tried to do it according to my teacher’s explanation, but I’m not getting the job done. I want you to go back to the beginning if the user answers wrong instead of just ending.
def paintcalculator():
print("What measurement unit do you use?")
unit = int(input("What measurement unit do you use?\n1 - Meters \n2 - Centimeters \n3 - Inches \nOption number:"))
if unit == 1: #meters
height = float(input('What is the height of the wall? '))
width = float(input('What is the width of the wall? '))
layers = float(input('How many layers do you want to use? '))
area = height * width
liters = 2 * area * layers
print('You will need aproximately {:.2f} liters of paint.'.format(liters))
elif unit == 2: #centimeters
height = float(input('What is the height of the wall? '))
width = float(input('What is the width of the wall? '))
layers = float(input('How many layers do you want to use? '))
area = (height*0.01) * (width*0.01)
liters = 2 * area * layers
print('You will need aproximately {:.2f} liters of paint.'.format(liters))
elif unit == 3: #inches
height = float(input('What is the height of the wall? '))
width = float(input('What is the width of the wall? '))
layers = float(input('How many layers do you want to use? '))
area = (height*0.0254) * (width*0.0254)
liters = 2 * area * layers
print('You will need aproximately {:.2f} liters of paint.'.format(liters))
else:
print('Please, answer correctly.')
return paintcalculator()
Contrary to what the answer below suggested, call the function
paintcalculator
within itself is not the best option as this is an improper use of recursion and this can cause problems (in addition to being an unnecessary complication). It is best to use a loop (like thewhile
for example). As it was not clear to me when to leave and when to continue, follow a suggestion/guess: https://ideone.com/bBFNp0– hkotsubo