Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python. if you can give an example that would be great. Basically, I need help w

ID: 3851527 • Letter: P

Question

Python. if you can give an example that would be great. Basically, I need help with making it so the user has to type y or n nothing else to continue or quit a program.

You have a long list of code and it performs a calculation. You ask the user at the end of the program another_calculation=input('Do you want to perform another calculation?(y/n):')

if (another_calculation_!='y')

do_calculation= False

The code runs

Do you want to perform another calculation ?(y/n):

The problem is you can type anything that is not a y to kill the program

How do you make it so that the user has to type y or n and if the user does not type n to quit the program it will keep repeating until the user types n or y. like I don't want the user typing in o instead of n to quit program?

Explanation / Answer

Python code:

def do_calculation():
pass
while True:
while True:
another_calculation = raw_input('Do you want to perform another calculation?(y/n):')
if another_calculation in ('y', 'n'):
break
print 'Invalid input.'
if another_calculation == 'y':
do_calculation()
continue
else:
print 'Calculation is completed !'
break