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()
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()
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()")
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()
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.