Write a while loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user if he or she wishes to perform the operation again. If so, the loop should repeat, otherwise it should terminate.
repeat = True
#Prompt and read the two numbers
while repeat:
firstNumber = float(input("Enter the first number: "))
secondNumber = float(input("Enter the second number: "))
#Calculate the sum
sum = firstNumber + secondNumber
print("The sum is:", sum)
#The loop should ask the user if he or she wishes to
#perform the operation again. If so, the loop should
#repeat, otherwise it should terminate.
userChoice = input("Do you want to perform the operation again? (y/n): ")
if userChoice.lower() != 'y':
repeat = False
Executed Output:
Enter the first number: 20
Enter the second number: 300
The sum is: 320.0
Do you want to perform the operation again? (y/n): y
Enter the first number: 25
Enter the second number: -5
The sum is: 20.0
Do you want to perform the operation again? (y/n): n