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

For loop with if statement in Python (Solved)

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;

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

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?

Thank you for your answer @luk2302

I just added while True and working well now.

Here is the new codes;

operation_list = [‘+’, ‘-‘, ‘x’, ‘/’, ‘**’, ‘root’]

while True:
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).")

>Solution :

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.

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