【问题标题】:Basic Calculator in Python [closed]Python中的基本计算器[关闭]
【发布时间】:2016-01-24 18:16:15
【问题描述】:

我是 Python 新手。我试图制作一个基本的计算器,但我真的找不到问题所在。它返回 0 退出代码,但什么也没有出现,没有输入,什么也没有。对此的任何帮助将不胜感激。谢谢。

def add(num1, num2):
    return num1 + num2

def subtract(num1, num2):
    return num1 - num2

def div(num1, num2):
    return num1/num2

def multi(num1,num2):
    return num1*num2

def main():
    operation = input("What do you want to do?(+, -, *, or /):")
    if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
        print("Your input is invalid. Please enter a valid input.")
    else:
        num1 = float(input("Enter value for num1: "))
        num2 = float(input("Enter value for num2: "))
        if (operation == "+"):
            print(add(num1, num2))
        elif (operation == "-"):
            print(subtract(num1, num2))
        elif (operation == "*"):
            print(multi(num1,num2))
        elif (operation == "/"):
            print(div(num1,num2))

    main()

【问题讨论】:

  • 请修正格式!

标签: python calculator


【解决方案1】:

根据上面的代码,您实际上从未运行过main()。现在,您已经说过main 的定义是提示用户,检查输入是否正确,然后进行数学运算。最后的main() 会导致程序在完成所有这些操作后重复(不确定是否需要循环)。

如果您不想循环,只想运行一次计算器,只需删除最后一个main() 的缩进,因为现在缩进意味着它在def main() 内部。只需将其向左移动到与def main(): 相同的缩进级别,您的程序应该可以正常运行。

【讨论】:

  • 非常感谢。你是最好的。有这样一个社区和像你这样的人来帮忙真是太好了。感激不尽。
【解决方案2】:

我认为你不见了:

if __name__ == "__main__":
    main()

您在 main 内对 main() 的调用本身不会执行,这可能就是您没有得到任何输入的原因。

除此之外,您的代码应该按预期工作(确保您不被零除;))。

编辑:为了让我的答案更明显,你应该这样做:

def main():
    operation = input("What do you want to do?(+, -, *, or /):")
    if (operation != "+" and operation != "-" and operation != "*" and operation != "/"):
        print("Your input is invalid. Please enter a valid input.")
    else:
        num1 = float(input("Enter value for num1: "))
        num2 = float(input("Enter value for num2: "))
        if (operation == "+"):
            print(add(num1, num2))
        elif (operation == "-"):
            print(subtract(num1, num2))
        elif (operation == "*"):
            print(multi(num1,num2))
        elif (operation == "/"):
            print(div(num1,num2))

if __name__ == "__main__":
    main()

【讨论】:

  • 仍然没有,同样的事情。以零退出代码退出且无输入。好像没有代码一样。
  • @BadrAlshaer 是您发布的代码 completefull 您运行的代码?
  • 据我现在所知,您的问题是您实际上并没有将我所说的代码放在正确的位置,否则它会起作用。
【解决方案3】:
num1=float(input("enter the first number :"))
op = input("sellect the operation :")
num2 = float(input("enter the second number :"))
if op== "+" :
    print(num1+num2)
elif op == "-":
    print(num1 - num2)
elif op == "*":
    print(num1*num2)
elif op == "/":
    print(num1 / num2)
else:
    print("please enter a real operation ")   
 #this one is more simple

【讨论】:

  • 简短的描述会很棒(:
【解决方案4】:

基本计算器:

方法一:

# This function adds two numbers 
def add(x, y):
    return x + y
# This function subtracts two numbers 
def subtract(x, y):
    return x - y
# This function multiplies two numbers
def multiply(x, y):
    return x * y
# This function divides two numbers
def divide(x, y):
    return x / y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Take input from the user 
choice = input("Enter choice(1/2/3/4): ")
num1 = float(input("Enter first number (Should be in numeric form): "))
num2 = float(input("Enter second number (Should be in numeric form): "))
if choice == '1':
    print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
    print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
    print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
    print(num1,"/",num2,"=", divide(num1,num2))
else:
    print("Invalid input")

方法二:

print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
# Take input from the user 
choice = input("Enter choice(1/2/3/4): ")
num1 = float(input("Enter first number (Should be in numeric form): "))
num2 = float(input("Enter second number (Should be in numeric form): "))
if choice == '1':
    print(num1,"+",num2,"=", num1+num2)
elif choice == '2':
    print(num1,"-",num2,"=", num1-num2)
elif choice == '3':
    print(num1,"*",num2,"=", num1*num2)
elif choice == '4':
    print(num1,"/",num2,"=", num1/num2)
else:
    print("Invalid input")

快乐学习...:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多