I've made a function that asks 3 questions to the user; taking into account the width, length and cost per tile ... which produces the total cost at the end of the function.
I want the function to go through the series of questions ensuring that the input is equal to an integer. However I do not want it to keep reverting back to the start of the questions.
eg.
If the user types in a string at 'What is the cost: '
I want it to re-ask THAT question and not to revert back to the first question in the series.
As the function is right now - it will keep returning to the first question of 'What is the width: ' if an integer is not entered.
"""
A function based on finding the cost of tiling a certain area; based
on
width, length and the cost per tile
"""
def tile_cost():
while True:
try:
w = float(input('What is the width: '))
l = float(input('What is the height: '))
c = float(input('What is the cost: '))
except ValueError:
print('This is not an int')
continue
else:
break
print("The cost of tiling the floor will be: £%.2f" % (w * l * c))
tile_cost()
I've tried multiple other ways of doing what i'm trying to achieve, but the codes gets messy and repeats itself and does not actually work for me. Having tried to search this for quite a while I've found it very difficult to find a definitive answer to the problem.
Thanks in advance for anyone's help, it will be massively helpful in understanding python further :)