Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Looping from topline in Python PyCharm

So I’m pretty new to Python, and am very interested. Right now, I’m trying to write a script to make a nice calculator. I have the base of the calculator, and it works quite well. Now, I just want it to ask the user if they would like to make another calculation. From what I’m thinking, it could go like:

  • First number input
  • Second number input
  • Calculation operation
  • Outcome
  • Would you like to restart?

So far, I have this:

first_number = input("First number: ")
second_number = input("Second number: ")
operation = input("(M)ultiply  (D)ivide   (S)ubtract  (A)dd: ")
if operation.upper() == "M":
    outcome = int(first_number) * int(second_number)
    print(outcome)
elif operation.upper() == "D":
    outcome = int(first_number) / int(second_number)
    print(outcome)
elif operation.upper() == "S":
    outcome = int(first_number) - int(second_number)
    print(outcome)
elif operation.upper() == "A":
    outcome = int(first_number) + int(second_number)
    print(outcome)

Could someone help me ask the new calculation question after the main part?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

So a nice way to do this is to add a while loop that checks if some variable we will call "runnextcalculation" is True. So, we initialize it to true (we want to run our first calculation for sure) Then we will collect input as described in your code. Then, in the end, we add a new input field that asks if the user would like to run another calculation. If this answer is "N" (No), we will set the runnextcalculation to false. When we return to the top of the while statement, runnextcalculation will force the while loop to terminate, and program will be done. If it is Y, the process will restart. Here is the code:

runnextcalculation = True
while runnextcalculation is True:
    first_number = input("First number: ")
    second_number = input("Second number: ")
    operation = input("(M)ultiply  (D)ivide   (S)ubtract  (A)dd: ")
    if operation.upper() == "M":
        outcome = int(first_number) * int(second_number)
        print(outcome)
    elif operation.upper() == "D":
        outcome = int(first_number) / int(second_number)
        print(outcome)
    elif operation.upper() == "S":
        outcome = int(first_number) - int(second_number)
        print(outcome)
    elif operation.upper() == "A":
        outcome = int(first_number) + int(second_number)
        print(outcome)
    ask_if_run_next_calculation = input("Would you like to run another calculation? (Y/N)")
    if ask_if_run_next_calculation == "N":
        runnextcalculation = False

Which outputs:

First number: 2
Second number: 2
(M)ultiply  (D)ivide   (S)ubtract  (A)dd: A
4
Would you like to run another calculation? (Y/N)Y
First number: 2
Second number: 2
(M)ultiply  (D)ivide   (S)ubtract  (A)dd: M
4
Would you like to run another calculation? (Y/N)N

Process finished with exit code 0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading