Home > Enterprise >  The result is not what I expected, python console problem
The result is not what I expected, python console problem

Time:07-07

new to coding, when enter an equation of 1 * 5, I get "You are ...lazy". But I need to get "You are ...very lazy". Can you help me finding the problem?

Expected:

You are ... lazy ... very lazy

5.0

Do you want to store the result? (y / n):

Found:

You are ... lazy

5.0

Do you want to store the result? (y / n):

    msg_0 = "Enter an equation"
    msg_1 = "Do you even know what numbers are? Stay focused!"
    msg_2 = "Yes ... an interesting math operation. You've slept through all classes, haven't you?"
    msg_3 = "Yeah... division by zero. Smart move..."
    msg_4 = "Do you want to store the result? (y / n):"
    msg_5 = "Do you want to continue calculations? (y / n):"
    msg_6 = " ... lazy"
    msg_7 = " ... very lazy"
    msg_8 = " ... very, very lazy"
    msg_9 = "You are"
    memory = 0


    def is_one_digit(v):
        v = float(v)
        if -10 < v < 10 and v.is_integer():
            return True
        else:
            return False


    def check(v1, v2, v3):
        msg = ""
        if is_one_digit(v1) and is_one_digit(v2):
            msg = msg   msg_6
        if (v1 == 1 or v2 == 1) and v3 == "*":
            msg = msg   msg_7
        if (v1 == 0 or v2 == 0) and (v3 == "*" or v3 == " " or v3 == "-"):
            msg = msg   msg_8
        if msg != "":
            msg = msg_9   msg
        print(msg)


    while True:
        calc = input(msg_0)
        try:
            x = calc.split()[0]
            oper = calc.split()[1]
            y = calc.split()[2]

            if x == "M":
                x = memory
            if y == "M":
                y = memory

            float(x)
            float(y)

            if oper in [" ", "-", "*", "/"]:
                check(x, y, oper)
                if oper == " ":
                    result = float(x)   float(y)
                    print(result)
                elif oper == "-":
                    result = float(x) - float(y)
                    print(result)
                elif oper == "*":
                    result = float(x) * float(y)
                    print(result)
                elif oper == "/":
                    if float(y) != 0:
                        result = float(x) / float(y)
                        print(result)
                    else:
                        print(msg_3)
                        continue

                user_input = input(msg_4)
                if user_input == "y":
                    memory = result
                    user_i = input(msg_5)
                    if user_i == "y":
                        continue
                    elif user_i == "n":
                        break
                    else:
                        user_i = input(msg_5)
                elif user_input == "n":
                    user_i = input(msg_5)
                    if user_i == "y":
                        continue
                    elif user_i == "n":
                        break
                    else:
                        user_i = input(msg_5)
                else:
                    user_input = input(msg_5)

            else:
                print(msg_2)
        except ValueError:
            print(msg_1)
            continue

CodePudding user response:

You call float(x) and float(y), but this is not saved anywhere, so v1 and v2 will be '1' and '5', instead of 1 and 5.

If you set

x = float(x)
y = float(y)

, it should work.

  • Related