I am writing code utilizing tuple for function call
def get_apple():
print('you got an apple.')
def get_pear():
print('you got a pear.')
fruits = (get_apple, get_pear)
print('0. apple\n1. pear')
s = eval(input('which one : '))
fruits[s]()
But once I execute this script, it only returns "TypeError: 'function' object is not subscriptable" on "fruitss".
Does anyone have an idea ?
get_appleandget_pear.evalat all cost because it's unsafe. Just useint(input('which one : ')). For example try typeexit()when prompted for input.get_apple[s], but that ain't the caseeval? Get rid of it; you know you wantsto be anint, so useint(input(...))instead.