简体   繁体   English

我怎样才能使这部分成为我的新代码循环?

[英]How can i make this part of my new code loop?

i want to make it so when you get your answer it automatically asks for another operator and another number until you type stop like real calculators我想这样做,所以当您得到答案时,它会自动询问另一个运算符和另一个数字,直到您像真正的计算器一样键入 stop

from math import*
info = input("Would you like information? : ")
if info == "yes":`enter code here`
    print("+ = add")
    print("- = subtract")
    print("* = multiplication")
    print("/ = division")
    print("pow/power/p = raise to power")
num1 = float(input("Enter your first number: "))
op = input("Enter your operator: ")
num2 = float(input("Enter your second number: "))
    if op == "+":
        print(num1 + num2)
    elif op == "-":
        print(num1 - num2)
    elif op == "*":
        print(num1 * num2)
    elif op == "/":
        print(num1 / num2)
    elif op == "pow" or "power" or "p":
        print(pow(num1, num2))
    else:
        print("Something went wrong please try again")
from math import *

info = input("Would you like information? : ")
if info == "yes":
    print("+ = add")
    print("- = subtract")
    print("* = multiplication")
    print("/ = division")
    print("pow/power/p = raise to power")

while True:
    num1 = input("Enter your first number: ")
    if num1 == "stop":
        break
    num1 = float(num1)
    op = input("Enter your operator: ")
    num2 = float(input("Enter your second number: "))
    if op == "+":
        print(num1 + num2)
    elif op == "-":
        print(num1 - num2)
    elif op == "*":
        print(num1 * num2)
    elif op == "/":
        print(num1 / num2)
    elif op == "pow" or "power" or "p":
        print(pow(num1, num2))
    else:
        print("Something went wrong please try again")
        
print("Code finished")

It obviously does not take edge cases into concideration (like division by zero, making sure that the stuff that you pass to float() is actually a number) but this should get you going:)它显然不会考虑边缘情况(比如除以零,确保传递给float()的东西实际上是一个数字),但这应该让你继续前进:)

cheers.干杯。

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

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