Checking if variable exists

Is there a way to check if a variable exists:

fruit = apple

so that when it checks for fruit it returns True but when it checks for vegatable it returns False
1 Like

Hey there, would you like to specify which language you’re using? (It’ll help giving a more specific answer)

Possible solution: (Assuming that you’re using python)

if 'fruit' globals():
  print('The fruit variable exists!')
else:
  print('The fruit variable doesnt exist!')

NOTE: This method is considered bad practice and should generally be avoided.
NOTE 2: This won’t work if you’re checking a local variable.

Oh i thought i put it on there sorry Python

ok thanks @MNA41 that’ll work

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.