-2

I made a input inside of a def function but need to use it in an if statement outside of the def function But it says the input is not defined

def mainmenu()
       Pick = input("input 1 for deposit, and so on")

If pick == "1":
   Deposit()
chepner
  • 497,756
  • 71
  • 530
  • 681

3 Answers3

0

It's hard to know what you're trying to do, but based on your example, you can use:

def pick():
   return input("input 1 for deposit, and so on")

if pick() == "1":
   # Deposit()
   print("execute Deposit()")

Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

you can do this is better approach instead to use if statement

menu_data = { '1' : func1, '2': func2} # etc...


def pick():
    user_input = input("input 1 for deposit, and so on")
    try:
      menu_data[user_input]() # call the method according to the input
    except KeyError as err:
        print('invalid key try again')
        pick()
Beny Gj
  • 607
  • 4
  • 16
0

Because the variable pick is only a local variable (one inside a function) and not a global variable (one that can be accessed everywhere), you have to return it to access the variable globally.

def pick():
  pick = input("input 1 for deposit, and so on")
  return pick

if pick() == "1":
  Deposit()
  # and so on...

Thus, you can access it by running the function. Then check if it is equal to one.

new Q Open Wid
  • 2,225
  • 2
  • 18
  • 34