0

I am trying to create a basic calculator in Python. I wrote all codes but i can not figure out to operate these codes for loop. Basically, after the calculation user have to go back start point and calculate again. Where should i put the "for loop" for these case?

Also you have to know that, I'm a little new to all this. Thank you.

Here is my codes;

operation_list = ['+', '-', 'x', '/', '**', 'root']

op = input(f"{operation_list}""Choose an Operation" + ":")

if op == "**":
    num4= int(input("Enter the number you want to exponent:"))
    num3 = int(input("Enter exponent:"))
    print(num4, "**", num3, "=", num4 ** num3)
    
elif op == "root":
    num5= int(input("Enter the Number You Want to Root:"))
    print(num5,"'in" " root", "=", num5 ** 0.5)

elif op == "+":
    num1= int(input("Enter First Number:"))
    num2= int(input("Enter Second Number:"))
    print(num1, "+", num2, "=", num1 + num2)
    
elif op == "-":
    num1= int(input("Enter First Number:"))
    num2= int(input("Enter Second Number:"))
    print(num1, "-", num2, "=", num1 - num2)
    
elif op == "x":
    num1= int(input("Enter First Number:"))
    num2= int(input("Enter Second Number:"))
    print(num1, "x", num2, "=", num1 * num2)
    
elif op == "/":
    num1= int(input("Enter First Number:"))
    num2= int(input("Enter Second Number:"))
    print(num1, "/", num2, "=", num1 / num2)
    
else:
    print("There is no such command. Please choose one of the following commands (+, -, x, /, **, root).")

Basically, after the calculation user have to go back start point and calculate again. Where should i put the "for loop" for these case?

1
  • while True: in the very beginning and indent everything by one level. And have a special case where you break out of the loop. Commented Jan 3, 2023 at 8:36

1 Answer 1

1

You have multiple choice. If you want the user to do 3 calculations for example, you can use a for loop like this :

for i in range(3):
    op = input(f"{operation_list}""Choose an Operation" + ":")
    if op == "+":
        num1= int(input("Enter First Number:"))
        num2= int(input("Enter Second Number:"))
        print(num1, "+", num2, "=", num1 + num2)
    else:
        print("There is no such command. Please choose one of the following commands (+, -, x, /, **, root).")

But if you want infinite calculations, you can use a while loop, but in this case I recommand to have a possibility to break the loop, by saying exit for example, like this :

while True:
    op = input(f"{operation_list}""Choose an Operation" + ":")
    if op == "+":
        num1= int(input("Enter First Number:"))
        num2= int(input("Enter Second Number:"))
        print(num1, "+", num2, "=", num1 + num2)
    elif op == "exit":
        break
    else:
        print("There is no such command. Please choose one of the following commands (+, -, x, /, **, root).")

Note that I simplified your code for readability, but I let you adapt to your case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.