0

To put it simply, I'm currently learning to make function call. I want to level it up a bit by making simple main menu selection for desired function, while accepting user input.

But the problem is, the code is stuck without returning anything. I can input value for x and y but then the code stops processing the math equations.

print ("Select function : ")
print ("1. Addition")
print ("2. Substraction")
print ("3. Multiplication")
print ("4. Exit")
print ("")
z = input("Input your selection here : ")
x = int(input("Enter x value : "))
y = int(input("Enter y value : "))

def add(x,y):
  return x + y

def min(x,y):
  return x - y

def mult(x,y):
  return x * y

loop = True

while loop:
  if z == '1':
    add(x,y)
  elif z == '2':
    min(x,y)
  elif z == '3':
    mult(x,y)
  elif z == '4':
    loop = False

Any advice to make it work? Thanks

martineau
  • 119,623
  • 25
  • 170
  • 301

4 Answers4

0

A few issues in your code:

  • You have to put your print statements inside the while loop inorder to show the menu to the user until the user exits.
  • min() is a reserved function in Python - It returns the minimum value of all the values passed to it. Don't Use it. Use some other name instead.
  • The reason you are not getting anything in output is you are not printing the return results of functions. Use print for all function calls inside your while loop [like this print(add(x,y)) ].

Code with all the above changes:

def add(x,y):
  return x + y

def diff(x,y):
  return x - y

def mult(x,y):
  return x * y

loop = True

while loop:
    print ("Select function : ")
    print ("1. Addition")
    print ("2. Substraction")
    print ("3. Multiplication")
    print ("4. Exit")
    print ("")
    z = input("Input your selection here : ")
    if z == '4':
        break
    else:
        x = int(input("Enter x value : "))
        y = int(input("Enter y value : "))
        if z == '1':
            print(add(x,y))
        elif z == '2':
            print(diff(x,y))
        elif z == '3':
            print(mult(x,y))
Ram
  • 4,724
  • 2
  • 14
  • 22
  • Thank you for the concise and insightful answer! It's interesting to see nested loop in use here. I'm still trying to understand the concept. – Michael Muliadi Jul 24 '21 at 12:39
0

You have a few issues.

  1. you are taking the input outside the loop, then you are entering into the loop. That way, loop won't be False anytime. The loop will go forever.

  2. you are returning the value but not printing it. use print(...)

  3. min is a built-in function. So don't define a new function called min

print ("Select function : ")
print ("1. Addition")
print ("2. Substraction")
print ("3. Multiplication")
print ("4. Exit")
print ("")


def add(x,y):
  return x + y

def minus(x,y):
  return x - y

def mult(x,y):
  return x * y


while True:
  z = input("Input your selection here : ")
  x = int(input("Enter x value : "))
  y = int(input("Enter y value : "))
  if z == '1':
    print(add(x,y))
  elif z == '2':
    print(minus(x,y))
  elif z == '3':
    print(mult(x,y))
  elif z == '4':
    break
0

The value of z and loop is the same throughout the loop so, it's looping infintely. You can try this:

print ("Select function : ")
print ("1. Addition")
print ("2. Substraction")
print ("3. Multiplication")
print ("4. Exit")
print ("")


def add(x,y):
  return x + y

def subs(x,y):
  return x - y

def mult(x,y):
  return x * y



while True:
    z = input("Input your selection here : ")
    if z == '4':
        break
    x = int(input("Enter x value : "))
    y = int(input("Enter y value : "))
    if z == '1':
        print(add(x,y))
    elif z == '2':
        print(subs(x,y))
    elif z == '3':
        print(mult(x,y))
  

You can you break as it's easier. You can add z==4 at front so you don't need to input other values. print to display values.

Bibhav
  • 1,579
  • 1
  • 5
  • 18
0

Not so bad for the first shot) The @Ram corrected the mistakes. But the real question is why the python input function doesn't have a filter option.

I've heard a lot of pain in that question

Perhaps if you don't want to get infinite scrolling of console. You could use the console module. It will available after executing pip install console in the terminal.

from sys import stdout
from console.utils import wait_key, clear_line, sc
from console.detection import get_position

print ("Select function : ")
print ("1. Addition")
print ("2. Substraction")
print ("3. Multiplication")
print ("4. Exit")
print ("")

def _input(prompt, result):
  if result is not None:
    cursor_pos = get_position()
    with sc.location(y = cursor_pos[1] - 1):
      clear_line()
      stdout.write('\r>>Last command result is %s' % result)
  clear_line()
  stdout.write('\r' + prompt)
  data = ''
  key = wait_key()
  while key != '\r':
    data += key
    stdout.write(key)
    stdout.flush()
    key = wait_key()
  return data

def add(x,y):
  return x + y

def diff(x,y):
  return x - y

def mult(x,y):
  return x * y

r = None

while True:
    z = _input("Input your selection here : ", r)
    if z == '4':
        break
    else:
        x = int(_input("Enter x value : ", r))
        y = int(_input("Enter y value : ", r))
        if z == '1':
            r = add(x,y)
        elif z == '2':
            r = diff(x,y)
        elif z == '3':
            r = mult(x,y)

print()
Vanya Usalko
  • 405
  • 7
  • 10