简体   繁体   English

Python 中带有 if 语句的 For 循环(已解决)

[英]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.我正在尝试在 Python 中创建一个基本计算器。我编写了所有代码,但我不知道如何循环操作这些代码。 Basically, after the calculation user have to go back start point and calculate again.基本上,计算完后用户必须将 go 返回起点重新计算。 Where should i put the "for loop" for these case?对于这些情况,我应该把“for 循环”放在哪里?

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.基本上,计算完后用户必须将 go 返回起点重新计算。 Where should i put the "for loop" for these case?对于这些情况,我应该把“for 循环”放在哪里?

Thank you for your answer @luk2302谢谢你的回答@luk2302

I just added while True and working well now.我刚刚添加 while True 并且现在运行良好。

Here is the new codes;这是新代码;

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

while True: op = input(f"{operation_list}""Choose an Operation" + ":") while True: op = input(f"{operation_list}""选择一个操作" + ":")

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).")

You have multiple choice.你有多项选择。 If you want the user to do 3 calculations for example, you can use a for loop like this:例如,如果您希望用户进行 3 次计算,您可以使用这样的 for 循环:

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循环,但在这种情况下我建议有可能打破循环,例如说exit ,像这样:

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.请注意,我简化了您的代码以提高可读性,但我让您适应您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM