Python乘法功能不起作用

时间:2018-04-21 16:02:00

标签: python-3.x

我是python的初学者想要创建一个简单的计算器应用程序,它为操作符'Add'和'Multiply'提供多个输入。我遇到了运算符'Multiply的问题,其中显示了错误的输出。所以任何人都可以帮助我使用语法。如果你帮我改进代码的其他部分,我也会感激不尽。

print("Type Add to add the numbers")
print("Type Subtract to subtract the numbers")
print("Type Multiply to multiply the numbers")
print("Type Divide to divide the numbers")

Operator = input()

if Operator == "Add":
    print("Type the number of numbers you want to add" )
    Num_Numbers = range(0,int(input()))
    Total = 0
    for Count in Num_Numbers:
        print("Type Num"+str(Count+1))
        Count = int(input())
        Total = Total + Count
    print(Total)


elif Operator == "Subtract":
    print("Type the first number")
    Num1 = float(input())
    print("Type the second number")
    Num2 = float(input())
    print(Num1 - Num2)

elif Operator == "Multiply":
    print("Type the number of numbers you want to multiply" )
    Num_Numbers = range(0,int(input()))
    Total = 0
    Counter = 0
    for Count in Num_Numbers:
        print("Type Num"+str(Count+1))
        count = int(input())
        if Counter != 0:
            Counter = Counter + 1
            while Total == 0:
                Total = Total + count
                print("Code implemented")
        else:
            continue
        Total = Total * count
    print(Total)

elif Operator == "Divide":
    try:
     print("Type the first number")
     Num1 = float(input())
     print("Type the second number")
     Num2 = float(input())
     print(Num1 / Num2)
    except ZeroDivisionError:
     print("Division by zero not possible")

else:
    print("Operator Unidentified!")

2 个答案:

答案 0 :(得分:0)

你把开始Total设为0. 0乘以任何值为0,所以你应该从1开始:

Total = 1

这比编程更重要。

您还需要删除下面显示的奇怪行(它们用于什么?!):

Counter = 0
#some needed lines
    if Counter != 0:
        Counter = Counter + 1
        while Total == 0:
            Total = Total + count
            print("Code implemented")
    else:
        continue

未来的注意事项:

var=var+1

写得更快:

var+=1

其他运营商也一样,并且:

else:
    continue

已过时,您 else

答案 1 :(得分:0)

elif Operator == "Multiply":
    print("Type the number of numbers you want to multiply" )
    Num_Numbers = range(0,int(input()))
    Total = 1.0
    for Count in Num_Numbers:
        print("Type Num"+str(Count+1))
        count = float(input())     #chances are the number can be float
        Total = Total * count
    print(Total)

简化您的代码!