David
David

Reputation: 13

Looping from topline in Python

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:

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?

Upvotes: 1

Views: 59

Answers (3)

Cole Agard
Cole Agard

Reputation: 51

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

Upvotes: 0

qkzk
qkzk

Reputation: 297

You can nest the whole script in a while loop like this.

When calculation is done, ask if the user wants to stop. Toggle a flag when he wants to quit.

cont = True
while cont:
    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)

    input_continue = input("(S)top :")
    if input_continue.upper() == "S":
        cont = False

Upvotes: 0

s.dallapalma
s.dallapalma

Reputation: 1315

You can handle that situation with a while.

operation = ""

while operation.upper() != "Q":
    first_number = input("First number: ")
    second_number = input("Second number: ")
    operation = input("(M)ultiply  (D)ivide   (S)ubtract  (A)dd   (Q)uit:")

    if operation.upper() == "M":
       # ...
    elif operation.upper() == "A":
       # ...

If "Q" is typed, at the end of the if/elif blocks (always false in this case), the code will exit the loop because condition operation.upper() != "Q" is False and end the program. This is the logic at glance. You can modify the script to add additional prints when you ask if the user desires to repeat the calculation.

Upvotes: 1

Related Questions