0

単純な電卓を作成しようとしていますが、使用している IDE が、文字列を使用して数学演算を実行しようとしていると表示し続けます。すべての変数を double に代入しようとしましたが、うまくいきませんでした。

 """Calculator program"""


loop = 1  # 1 means loop; anything else means don't loop.
choice = 0  # This variable holds the user's choice in the menu
add1 = 0.0
add2 = 0.0
sub1 = 0.0
sub2 = 0.0
mul1 = 0.0
mul2 = 0.0
div1 = 0.0
div2 = 0.0
result = 0.0
while loop == 1:
    # Print the options user has
    print ("Welcome to calculator.py")

    print ("your options are:")
    print (" ")
    print ("1) Addition")
    print ("2) Subtraction")

    print ("3) Multiplication")

    print ("4) Division")
    print ("5) Quit calculator.py")
    print (" ")
    #Perform the desired operation
    choice = int(input("Choose your option: ").strip())
    if choice == 1:
        add1 = input()
        add2 = input()
        result = add1 + add2
        print(add1, add2)
        print (add1, "+", add2, "=", result)
    elif choice == 2:
        sub2 = input()
        sub1 = input()
        result = sub1 - sub2
        print (sub1, "-", sub2, "=", result)
    elif choice == 3:
        mul1 = input("Multiply this: ")
        mul2 = input("with this: ")
        result = mul1 * mul2
        print (mul1, "*", mul2, "=", result)
    elif choice == 4:
        div1 = input("Divide this: ")
        div2 = input("by this: ")
        result = div1 / div2
        print (div1, "/", div2, "=", result)
    elif choice == 5:
        loop = 0

print ("Thank-you for using calculator.py!")
4

2 に答える 2

3

input()数値ではなく文字列を返します。それを変換する必要があります:

add1 = int(input())

また

add1 = float(input())

電卓に何をサポートさせたいかによって異なりますが、それ以外の場合は実際に文字列を使用して数学演算を実行しています。

于 2013-02-28T16:08:55.273 に答える
0

貼ってみて

    int()

その上で作る

    int(input))

お役に立てれば

于 2013-02-28T16:48:56.683 に答える