Python Forum
Assigning a new value to variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning a new value to variable
#1
How can I assign the result (res) of the calulation to the variable "memory" and then check if the user input is "M" and if so, then set
x or y = M
??

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):"
oper = ['+', '-', '*', '/']
memory = 0


def solution():
    global x
    global y
    global operation
    while True:
        print(msg_0)
        calc = input()
        lst = calc.split(' ')
        operation = lst[1]
        x = lst[0]
        y = lst[2]
        if x == str('M'):
            x = memory
        elif y == str('M'):
            y = memory
        try:
            x = float(lst[0])
            y = float(lst[2])
        except ValueError:
            print(msg_1)
            continue
        try:
            if operation in oper:
                break
            else:
                print(msg_2)
        except ValueError:
            continue


def calculations():
    global res
    res = 0
    if operation == oper[0]:
        res = x + y
        return res
    elif operation == oper[1]:
        res = x - y
        return res
    elif operation == oper[2]:
        res = x * y
        return res
    elif operation == oper[3] and y != 0:
        res = x / y
        return res
    else:
        print(msg_3)
        solution()


def store_res():
    print(msg_4)
    answer = input()
    if answer == 'y':
        memory = res
        answer_on_msg_5()
        return memory
    elif answer != 'n':
        store_res()
    else:
        answer_on_msg_5()


def answer_on_msg_5():
    print(msg_5)
    answer_1 = input()
    if answer_1 == 'y':
        solution()
    elif answer_1 != 'n':
        answer_on_msg_5()
    else:
        print()


solution()
calculations()
print(calculations())
store_res()
Reply
#2
or is the logical operator.

666+666*666/666-666
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  assigning a variable :( gr3yali3n 0 1,321 Sep-22-2020, 09:02 PM
Last Post: gr3yali3n
  Assigning variables Godserena 4 2,192 Apr-26-2020, 06:59 AM
Last Post: buran
  Assigning a Variable Help MC2020 5 2,953 Jan-06-2020, 10:54 PM
Last Post: MC2020
  Assigning an item from a list xlev 1 1,459 Sep-27-2019, 04:42 PM
Last Post: Larz60+
  assigning index 3Pinter 6 3,006 Jan-18-2019, 10:07 PM
Last Post: nilamo
  Not adding and assigning? 00712411 7 4,191 Oct-10-2018, 07:11 PM
Last Post: volcano63
  Assigning iter_row value to variable ankey 8 9,067 Sep-24-2018, 03:51 PM
Last Post: ankey
  Assigning a new variable in a IF loop pythoneer 5 3,791 Mar-02-2018, 05:21 AM
Last Post: pythoneer
  Assigning to string slice michaeljhuman 1 2,764 Feb-08-2018, 12:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020