Andre Korol - 1 year ago 135

Python Question

I'm still starting to learn Python on my own and I need to write a script that asks the user for 4 numbers and then it lets the user choose which math operations it is going to perform with the chosen numbers. I already defined the math operations functions and asked the user for the numbers and set variables with them:

`def add(a, b):`

return a + b

def subtract(a, b):

return a - b

def multiply(a, b):

return a * b

def divide(a, b):

return a / b

x = float(raw_input('Choose your first number\n> '))

y = float(raw_input('Choose your second number\n> ' ))

z = float(raw_input('Choose your third number\n> '))

w = float(raw_input('Choose your fourth number\n> '))

Now I need to ask the user which operations he would like to perform in order of choice like:

If he says add then the script adds x + Y, if for the second operator he says multiply the scripts then multiply the sum of (x+y) by z and if for the third operator he says divide the script takes ((x+y)*z) and divide it by w.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

If I understand the problem correctly, I would ask a user what operation he wants to go with:

```
math_option =raw_input('Choose the math operation: ')
```

and later on check what option was chosen:

```
if math_option == "add":
print add(x, y)
if math_option == "multiply":
add_num = add(x,y)
mul_num = multiply(math_option,z)
print mul_num
```

and so on

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**