Voilà, Better!!
def f_input():
leng = int(input("length: "))
width = int(input("Width: "))
width = int(input("Height: "))
return leng, width, width
def ftest_try():
try:
leng, width, height = f_input()
test_input = True
except:
test_input = False
return test_input, leng, width, height
def ftest_answer(question, answer):
while True:
choice = input(question)
if choice.lower() == 'y':
return True
elif choice.lower() == 'n':
return False
else:
print(answer)
def ftest_input():
test_input, leng, width, height = ftest_try()
while test_input == False or leng == 0 or width == 0 or height == 0:
print('Please enter a good value!')
test_input, leng, width, height = ftest_try()
return leng, width, height
#************ Main program **************************
choice1 = "Y to confirm / N to exit: "
choice2 = "Do you want to continue? (Y/N): "
leng, width, height = ftest_input()
test_answer = ftest_answer(choice1, choice1)
while test_answer:
print('Parallelepiped volume: ', leng * width * height)
test_answer = ftest_answer(choice2, choice2)
if test_answer:
leng, width, height = ftest_input()
test_answer = ftest_answer(choice1, choice1)
print('Good bye')
We can make better??
When I enter 2 or more letters in the question, for example :
“Y to confirm / N to exit: NNN
Y to confirm / N to exit:
Y to confirm / N to exit:”
The question returns twice. I change this :
def ftest_answer(question, answer):
while True:
choice = input(question)
if choice.lower() == 'y':
return True
elif choice.lower() == 'n':
return False
else:
print(answer)
by this:
while True:
choice = input(question)
if choice.lower() == 'y':
return True
elif choice.lower() == 'n':
return False
And now it's Ok!!