How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Tony M Your Own Question
Tony M
Tony M, Computer Software Engineer
Category: Programming
Satisfied Customers: 41
Experience:  BSE Computer Engineering, over 20 years programming experience (various platforms)
Type Your Programming Question Here...
Tony M is online now
A new question is answered every 9 seconds

design do-while loop that asks the user to enter two numbe

Customer Question

design do-while loop that asks the user to enter two numbers
Submitted: 8 years ago.
Category: Programming
Expert:  Ansh P. replied 8 years ago.



In which language?

Customer: replied 8 years ago.
Expert:  Ansh P. replied 8 years ago.
Sorry I will opt out to let others experts help you.
Customer: replied 8 years ago.
Design a Do-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.

all in python code
Expert:  Tony M replied 8 years ago.

Here is an example; a few notes:

- this is python 3.0; if you need an earlier version, let me know
- the loop continues as long as "Y" or "y" is entered for continue; anything else exits the loop
- this is a "while" loop - "do" isn't part of python, but the "while" loop, as constructed, performs all the functionality that "do" would entail; specifically, having the condition set to true (cont='y') before the loop starts creates an initial "do" condition so that the loop will execute at least once

--- python code:

n1 = 0
n2 = 0
cont = 'y'
while (cont == 'y' or cont == 'Y'):
     n1 = int(input("Please enter first number: "))
     n2 = int(input("Please enter second number: "))
     print (n1, "+", n2, "=", n1+n2)
     cont = input("Repeat (y/n)? ")

Expert:  Tony M replied 8 years ago.

Did this script work for you? It ran fine on my computer.